Timestamp does not show correct value for my timezone


I am having some trouble with the timestamps and dates in general.

My time zone is UTC (Europe/Lisbon) but when DST kicks in the time changes to UTC+1

The problem is that node-red does not take the DST in to account. So now whenever I create a date its created with the UTC time.

I also used the moment.js library, but get the same problem. This is where things get wierder.

I set up a timestamp inject that runs a function node with the following code:

var moment=global.get('moment')
msg.nowlocal=moment().local() //current datetime in local
msg.now=moment() //current datetime
msg.utc=moment().utc() //current datetime in utc
msg.nowlocaloffset=moment().local().utcOffset() //difference from current datetime local to UTC
msg.nowoffset=moment().utcOffset() //difference from current datetime to UTC
msg.utcoffset=moment().utc().utcOffset() //difference from UTC datetime to UTC - has to be 0
return msg

Using the local() should get me my timezone date, and the utc() should get me the UTC. The utcOffset() gives me the difference between the date generated and UTC time.

The response is the following:

The current time is 2019-04-11T07:59:13.702Z. How can the nowlocal and now dates have the same timestamp as the utc one and still report a +60minute shift from it?

Can anyone help me?

Thanks in advance.

Check to see what the timezone is set to on the machine you are running Node-RED server on. As Node-RED picks up the timezone from there,

If you are doing any database work it usually makes sense to leave the timezone as UTC so you don't get double entries / no entries when the timezone changes.

The machine timezone is correct:

Current default time zone: 'Europe/Lisbon'
Local time is now: Thu Apr 11 09:04:15 WEST 2019.
Universal Time is now: Thu Apr 11 08:04:15 UTC 2019.

Even if it was wrong, that would not explain how two dates can have the same datetime value and have different utcoffsets - nowlocal and now vs utc in my example.

Since this problem appeared after Daylight Saving Time started, the issue must have something to do with it.

When you use moment().local() which javascript function do think is being called by .local()?

I think it is this one:


Notice the wording of that
"Sets a flag on the original moment to use local time to display a moment instead of the original moment's time".
It does not change the time, it just says that if, for example, you use moment's methods to get the number of hours then it will perform that conversion using the timezone data. Since you have not performed that sort of operation on it then there is no difference between them. If you were to look at msg.nowlocal.hours() and msg.utc.hours() then I think they should be different.
When you displayed the response, which I guess was via a debug node, then it has only seen the underlying Date object rather than the moment object that wraps the date, since a debug node does not know about moment objects, so when it converted the different flavours to strings for display it did not even see the flag, so it displayed them all the same.

You are correct.

That was the issue.

By using the format() method on the moment I was able to get the "wrapped" date instead of the date object in the debug node.

var moment=global.get('moment')




return msg


"utc":"2019-04 11T12:54:31Z",

Thank you.

Don't forget that there is also a moment node that may make things simpler for you (or may not depending on your use!)

Thank you, but this flow in particular is getting too cluttered. Since this is something that can be done easily in a function node along with other actions, I believe using the library is better suited.

1 Like

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