0

I'm using Luxon to format a DateTime, and I need to it to be the following format, including the 'T' and 'Z' characters:

20150830T123600Z

I've tried to format the DateTime using:

let dateTimeNow = DateTime.now();
let formattedDateTime = dateTimeNow.toFormat('yyyyMMddTHHmmssZ');

But I get the format:

2021-05-25T12:43:37.043Z 

How do I remove the '-', ':' and '.' symbols?

Mark
  • 11
  • 3
  • [related?](https://stackoverflow.com/q/8362952/10197418) - the OP there asks for a different format, but you should be able to adjust the solution to your needs. – MrFuppes May 25 '21 at 13:18
  • @MrFuppes not quite what I'm looking for but could work. It's a bit hacky though. – Mark May 25 '21 at 13:49
  • Without Luxon: `new Date().toISOString().replace(/(\.\d{3})|\W/g,'')`. – RobG May 25 '21 at 20:17

1 Answers1

0

You may escape strings using single quotes (Doc: Escaping), so you can use toFormat("yyyyMMdd'T'HHmmss'Z'").

Please note that the Z at the ends stands for UTC+0 offset, so I suggest to do not use it to represent local times with different offset.

Example:

const DateTime = luxon.DateTime;
let dateTimeNow = DateTime.utc();
let formattedDateTime = dateTimeNow.toFormat("yyyyMMdd'T'HHmmss'Z'");
console.log(formattedDateTime)
<script src="https://cdn.jsdelivr.net/npm/luxon@1.26.0/build/global/luxon.js"></script>
VincenzoC
  • 24,850
  • 12
  • 71
  • 90