How to get the current date and time in workflow

Describe the problem/error/question

How I can get the current Date and time? and change it into ISO 8601 date and time format.

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

with
{{ $now }} I get the current Date and Time,

Mon Oct 23 2023 18:44:51 GMT+0530 (India Standard Time)

I want to convert it into

2023-10-23T13:14:51.000Z

(ISO 8601 representation)

How It can be done in the workflow??

Also,

How to convert

2023-10-23T13:26:28.890+00:00

into

2023-10-23T13:14:51.000Z

Hi @Gouravdev :wave: You figured out you can use $now - but if you use {{$now.toISO()}} as an expression, you’ll convert it to ISO which will be the final format you’re looking for :slight_smile:

Hi @EmeraldHerald

Thanks for the reply but It’s not working

I need the output in this format

2023-10-23T13:14:51.000Z

Ah, sorry! The quickest way to do this would probably be to add replace('+00:00', 'Z')}} at the end of your conversion. For me it would be {{$now.toISO().replace('+01:00', 'Z')}}, for example, and provides this:

Hi - I had to work between UTC and ISO8601 dates in a workflow, and the MomentJS library can actually handle this natively, I think what you want to use is DateTime.utc().toISO(). This will always work regardless of timezone, whereas search/replace will introduce an error if the server time is ever different from the one expected during workflow creation.

Here’s what that looks like given the current time for where I am:

$now(): Wed Oct 25 2023 13:41:13 GMT+1300 (New Zealand Daylight Time)
$now.toISO(): 2023-10-25T13:41:13.403+13:00
Datetime.utc().toISO(): 2023-10-25T00:41:13.403Z

MomentJS Docs for the above:

3 Likes

Thanks guys :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.