How to format date like this?

Hello

I want to format dates such as follows:

Like this: 2022-05-26T00:00:00Z

That is (ignore the plus sign) : today’s date + T+ 00:00:00Z (always 00:00:00Z at the end)

And another date like this:

2022-05-26T15:00:35.844000Z

that is (ignore the plus sign) : today’s date + T + HH:MM:SSSSSS+Z

So far I have this: {{new Date().toISOString()}}

See node below:

Im struggling to tune it in a way that outputs the dates in the formats above.
Thank you in advance for your help.

Hi @th3liam, I hope you’re having a good day?

For the transformations you have described I’d suggest using Luxon inside a Set node over the Date & Time node. This is because the Date & Time node would not be able to mix an date (like 2022-05-26 in your first example) with a pre-defined string like T00:00:00Z.

This is how a Set node returning today’s date in your desired format could look like:

In this example I am mixing an expression using Luxon to get today’s date (that’s the part inside {{ and }}) with the hard-coded part T00:00:00Z:

The second part could look very similar. Assuming you want to use the current time, your expression could look like so:

{{ $now.setZone('UTC').toFormat('yyyy-MM-dd') }}T{{ $now.setZone('UTC').toFormat('hh:mm:ss.SSS') }}000Z

The one thing Luxon does not support from the looks of it are 6-digit fractional seconds. So this example only uses milliseconds (indicated by SSS) and then adds three 0s. Hope this still works for you!

3 Likes

Thank you @MutedJam!
I believe I’d seen that article before but didn’t make much sense out of it, now I do. :slight_smile: