Function node - weird date behaviour?

I have a function node with the following:

node.warn(new Date(2024, 4, 1))
node.warn(new Date(2024, 4, 1).toJSON())


function : (warn)
"Wed May 01 2024 00:00:00 GMT+0200 (Central European Summer Time)"

function : (warn)

I must be missing something, but what goes wrong here ?
(corrected the first output, copied the wrong one)

There nothing wrong here.

The first implicitly uses Date toString(), so the output is formatted according to the locale.

On the other hand, toJSON() uses toISOString(), which results in UTC in ISO 8601 notation.

For reference: Date.prototype.toJSON() - JavaScript | MDN

1 Like

Yes, but the first is first of May, second is 30th of April.

Because the time of the date is 0:00 in UTC+2. (note: your local timezone)
If converted to UTC this results in the prior day at 22:00.

Nothing wrong here, just two different timezones :slightly_smiling_face:

Ah, missed to process that part in the brain, got it thanks!

Yes, it's a bit tricky.
If you don't specify the timezone when using the Date function or constructor, the local timezone is used.

I recommend using UTC where possible to avoid those problems.

1 Like

nothing is wrong it looks perfect

what do you want to do?

Indeed - for this function i needed to get the first and last date of each month, I now specified 10:00 and then i hopefully will be good, but will make it a bit more robust.

I found this at stackoverflow which uses no time at all.

Indeed i used something similar, but wanted to check the output (as i need to calculate the date to another number) and found this difference, which was basically self-inflicted I guess :wink:

const getMonthInfo = () => {
    const currentDate = new Date();
    const firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
    const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 0);
    const monthName = currentDate.toLocaleString('default', { month: 'long' });

    return {
        nr: currentDate.getMonth() + 1,
        name: `${monthName} - First: ${firstDayOfMonth.toDateString()}, Last: ${lastDayOfMonth.toDateString()}`
return { payload: getMonthInfo()}
payload: object
nr: 5
name: "May - First: Wed May 01 2024, Last: Fri May 31 2024"

Ah sorry, quite - you are already using just date. Weird how JS adds a time to a date - other languages don't do that, instead they have a "pure" date type as opposed to a datetime type or just a time type.

1 Like

JS date handling and processing is a known problem drenched in history and ...

Hopefully it won't be too much longer when we can switch to temporal (and get rid of (or at least reduce the need for) all the other libs like moment!)

FWIW, you can use temporal today if you desire: @js-temporal/polyfill - npm


Thanks for that, learnt something new: polyfill:

What makes a polyfill different from the techniques we have already, like a shim, is this: if you removed the polyfill script, your code would continue to work, without any changes required in spite of the polyfill being removed.

Didn't know.


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