# Timestamp does not show correct value for my timezone

**URL:** <https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010>\
**Category:** General\
**Created:** [11 April 2019 07:10 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010 "2019-04-11T07:10:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![diogodtway](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diogodtway/32/8127_2.png) [@diogodtway](https://discourse.nodered.org/u/diogodtway)\
**Post date:** [11 April 2019 07:10 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/1 "2019-04-11T07:10:07Z")

</div>

Hello,

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:  
{"\_msgid":"56f0ffb6.40749",  
"topic":"","payload":1554965953702,  
"nowlocal":"2019-04-11T06:59:13.702Z",  
"now":"2019-04-11T06:59:13.702Z",  
"utc":"2019-04-11T06:59:13.702Z"  
,"nowlocaloffset":60,  
"nowoffset":60,  
"utcoffset":0}

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.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [11 April 2019 07:18 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/2 "2019-04-11T07:18:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![diogodtway](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diogodtway/32/8127_2.png) [@diogodtway](https://discourse.nodered.org/u/diogodtway)\
**Post date:** [11 April 2019 08:06 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/3 "2019-04-11T08:06:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 April 2019 08:27 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/4 "2019-04-11T08:27:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![diogodtway](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diogodtway/32/8127_2.png) [@diogodtway](https://discourse.nodered.org/u/diogodtway)\
**Post date:** [11 April 2019 10:32 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/5 "2019-04-11T10:32:20Z")

</div>

I think it is this one:

[https://momentjs.com/docs/#/manipulating/local/](https://momentjs.com/docs/#/manipulating/local/)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [11 April 2019 12:41 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/6 "2019-04-11T12:41:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![diogodtway](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diogodtway/32/8127_2.png) [@diogodtway](https://discourse.nodered.org/u/diogodtway)\
**Post date:** [11 April 2019 12:55 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/7 "2019-04-11T12:55:58Z")

</div>

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')

now=moment()

msg.nowlocal=moment().local().format()  
msg.now=moment().format()  
msg.utc=moment().utc().format()  
msg.nowlocaloffset=moment().local().utcOffset()  
msg.nowoffset=moment().utcOffset()  
msg.utcoffset=moment().utc().utcOffset()

msg.hoursnow=moment().local().hours()  
msg.hoursutc=moment().utc().hours()

return msg

debug:

{"\_msgid":"6432ecdf.299224",  
"topic":"","payload":1554987271150,  
"nowlocal":"2019-04-11T13:54:31+01:00",  
"now":"2019-04-11T13:54:31+01:00",  
"utc":"2019-04 11T12:54:31Z",  
"nowlocaloffset":60,  
"nowoffset":60,  
"utcoffset":0,  
"hoursnow":13,  
"hoursutc":12}

Thank you.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [12 April 2019 16:09 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/8 "2019-04-12T16:09:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![diogodtway](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/diogodtway/32/8127_2.png) [@diogodtway](https://discourse.nodered.org/u/diogodtway)\
**Post date:** [15 April 2019 07:04 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/9 "2019-04-15T07:04:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [29 September 2020 07:38 UTC](https://discourse.nodered.org/t/timestamp-does-not-show-correct-value-for-my-timezone/10010/10 "2020-09-29T07:38:37Z")

</div>

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