How can I use now() in a expression

Hi dears

I want to set a variable to now() time also I want to format it to special ones.
can anyone help me please?

You can simply use any kind of JavaScript in the curly brackets. So depending what you need you could do this:

{{new Date().getTime()}}
// Output: 1573041058024
{{new Date().toISOString()}}
// Output: 2019-11-06T11:50:58.025Z
{{new Date().toString()}}
// Output: Wed Nov 06 2019 12:50:58 GMT+0100 (Central European Standard Time)
{{new Date().toLocaleDateString()}}
// Output: 11/6/2019
9 Likes

Dear @jan
For my variable I set :face_with_monocle:{{new Date().toLocaleString(“sv-SE”, {timeZone: “…”})}}
and it return:
2019-11-06 23:32:11

above returned is my goal.
But unfortunately in json it return something else (Below image). Why?

1 Like

It looks like node.js gives a different output than the browser. It talkes about it here:

When I find some time (can sadly not say when) I will check it out and see if any of the proposed solutions work here.

1 Like

Dear @jan
Is there any way to solve this issue probably?

Best regards;

You can use a function node and import moment and get the now, then you can do whatever you want with that output. Do you necessarily have to use it in an expresión? @mooghermez

1 Like

And like this but with microseconds instead of millis?

Hey @xmontero, this is a rather old thread and n8n has changed a lot since. So you might want to open a new topic next time you come across such a question :slight_smile:

Using microseconds in JavaScript isn’t something I’d recommend as the implementation differs depending on the environment. There definitely is no straightforward way to get such a string using Luxon, which is the library n8n uses for time formatting these days. So in most cases I’d simply append 000 to my millisecond value if any API would require nanoseconds.

You could check out this older SO thread for more information on the topic, I think the last comment really points out the problems with this:

The answer is “no”, in general. If you’re using JavaScript in some server-side environment (that is, not in a browser), then all bets are off and you can try to do anything you want.

edit — this answer is old; the standards have progressed and newer facilities are available as solutions to the problem of accurate time. Even so, it should be remembered that outside the domain of a true real-time operating system, ordinary non-privileged code has limited control over its access to compute resources. Measuring performance is not the same (necessarily) as predicting performance.

editing again — For a while we had performance.now(), but at present (2022 now) browsers have degraded the accuracy of that API for security reasons.

You can manipulate the ISO string to produce a short-form, e.g. the timestamp for older MySQL versions

{{ new Date().toISOString("en-GB").replace('T', ' ').slice(0, -5) }}

“2022-11-11 14:56:42”

4 Likes

@DerekC

{{ new Date().toISOString(“en-GB”).replace(‘T’, ’ ').slice(0, -5) }}

How can I use this to showcase a particular Timezone? I realize it only shows GMT by default

Hi @KevinK, you might want to consider using Luxon over the default JS date. This will make time formatting much easier. For example {{ $now.setZone('Asia/Tokyo').toFormat('yyyy-MM-dd HH:mm:ss') }}

.setZone() sets the timezone, .toFormat() specifies the format of your timestamp. If you plan to use your value with other system or perform a comparison you want to use a standardized format though, which you could get with something like {{ $now.toISO() }}.

Going forward could you perhaps consider opening new threads for new questions? This one is rather old and already marked as solved, making it easy to miss new questions posted in here. Thank you so much!

3 Likes

Thanks! This did the trick;
{{ $now.setZone('Asia/Tokyo').toFormat('yyyy-MM-dd HH:mm:ss') }}

3 Likes

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