# Using the time in node red

**URL:** <https://discourse.nodered.org/t/using-the-time-in-node-red/16761>\
**Category:** General\
**Created:** [16 October 2019 08:21 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761 "2019-10-16T08:21:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 08:21 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/1 "2019-10-16T08:21:13Z")

</div>

Hi all, Ive been playing with node red for a while to set up to home automation. Its all going well and is up and running but I noticed this morning things happening at an unexpected time and I found this is due to the time in node red not being the same time zone as the raspberry pi that its running on.

I found the following discussion and followed the guide in the last post, I have added  
process.env.TZ = "Europe/London";

to the settings.js file and this didnt resolve it.

> [@Timezone settings?](https://discourse.nodered.org/t/timezone-settings/1176):
>
> Hi, I have a function which includes a line similar to this new Date(1530215497000) (The epoch datestamp used here is an example) and al though the server on which Node-red runs is set to the correct timezone, it is not respected and (since I’m in GMT +2) is consistently 2 hours off. What am I missing here or what else can I do to fix this? cheers

I have copied the function to a simple page to post here. Hopefully this is formatted correctly.

Thanks

`[{"id":"52175ebb.b4914","type":"tab","label":"Time Test","disabled":false,"info":""},{"id":"6bbeb4e0.742adc","type":"function","z":"52175ebb.b4914","name":"time","func":"var now = msg.now;\n\nvar dateTime = new Date(now).toLocaleString();\nvar dateTimeStamp = new Date(now);\n\nmsg.r = {\n time_locale : dateTime,\n time_timestamp : dateTimeStamp\n}\nreturn msg;","outputs":1,"noerr":0,"x":550,"y":300,"wires":[["bd43b9f6.9e6fa8"]]},{"id":"58325b00.b3d8a4","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":300,"wires":[["5bd53355.d3c3fc"]]},{"id":"bd43b9f6.9e6fa8","type":"debug","z":"52175ebb.b4914","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":300,"wires":[]},{"id":"5bd53355.d3c3fc","type":"change","z":"52175ebb.b4914","name":"timestamp","rules":[{"t":"set","p":"now","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":300,"wires":[["6bbeb4e0.742adc"]]}]`

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 08:24 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/2 "2019-10-16T08:24:05Z")

</div>

Also I have just checked the settings.js file again and I added that line right at the end outside of the moduel.exports obect. I dont know if thats the correct place for it to go.

And this is the output I get from the time stamps

```auto
{
"time_locale":"10/16/2019, 9:28:01 AM",
"time_timestamp":"2019-10-16T08:28:01.624Z"
}

```

---

<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:** [16 October 2019 08:35 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/3 "2019-10-16T08:35:41Z")

</div>

So currently in the UK there is British Summer Time.  
So the time locale is 9 something  
and the Zulu time (UTC/GMT) is 8 something.  
(the Z at the end indicates it is UTC)

What did you expect to see?

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 08:40 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/4 "2019-10-16T08:40:00Z")

</div>

I worked out that that was the issue, but I dont know how to set up the zone correctly in node red. I thought it would take the time from the pi itself but the time in the pi is correct.

If I take a now timestamp from inside node red and make a date object out of it, its an hour behind.

How do I correct the time zone so that the now timestamp is accurate?

---

<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:** [16 October 2019 08:53 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/5 "2019-10-16T08:53:42Z")

</div>

What makes you think it is wrong? The two times you posted are the same time, 9:28 local time and 8:28 UTC are just different ways of displaying the same time.

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 08:58 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/6 "2019-10-16T08:58:59Z")

</div>

node red thinks its an hour earlier than it is, so the schedule using this time starts and stops an hour earlier than it is set to.

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 09:05 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/7 "2019-10-16T09:05:06Z")

</div>

I think I have misunderstood the timestamp, it is a time since the start of the epoch, so that wont change based on locale. I think thats where Im getting confused. Im looking at a representation of that time based on locale and seeing the difference but it isnt incorrect.

I want the to use the local time, not the unix time, so I will need to convert it first and then make use of it. I think....

---

<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:** [16 October 2019 10:03 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/8 "2019-10-16T10:03:47Z")

</div>

What do you want to do with the time?

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 10:15 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/9 "2019-10-16T10:15:06Z")

</div>

Its going into a schedule function to set an occupancy state.

I have worked around it now. Ive checked the Date object for a timezone offset, if it exists it will offset the schedule. This is all working by the look of things. I'm using schedex module which has offsets available to modify. I have it working now, and have tested with some fixed dates at GMT and BST and not seeing any errors 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:** [16 October 2019 10:20 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/10 "2019-10-16T10:20:33Z")

</div>

> [@steven\_191](#):
>
> I'm using schedex module

The schedex node (I presume you mean node-red-contrib-schedex) should know about the local time zone and any times you enter in the config are expected to be in local time, if it isn't then something odd is going on. Are you sure the time on the pi is correct? What does  
`date`  
show?

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 10:23 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/11 "2019-10-16T10:23:30Z")

</div>

It shows the correct local time in BST. I rememeber being very thorough to make sure this was correct before I started using node red, knowing that the time was going to be a part of what I wanted to use.

---

<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:** [16 October 2019 10:25 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/12 "2019-10-16T10:25:39Z")

</div>

What _exactly_ does `date` show and what is the problem you are having with the schedex node? Export a mini flow here showing the problem if possible.

**[Edit]** Also what version of nodejs are you using? Run `node -v` to find out. In fact if you post the output you get when you start node red in a terminal that might be helpful.

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [16 October 2019 10:45 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/13 "2019-10-16T10:45:21Z")

</div>

in the flow function called 'time' I manually set the offset variable to 0 to simulate GMT or set a fixed date in dt.

```auto
pi@raspberrypi:~ $ date
Wed 16 Oct 11:39:34 BST 2019

```

```auto
pi@raspberrypi:~ $ node -v
v10.15.3

```

```auto
[{"id":"52175ebb.b4914","type":"tab","label":"Time Test","disabled":false,"info":""},{"id":"1e252ea2.2d5891","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":300,"wires":[["f1ee5f0.0ed11a"]]},{"id":"f1ee5f0.0ed11a","type":"function","z":"52175ebb.b4914","name":"time","func":"var now = new Date();\nvar offset = now.getTimezoneOffset();\n//offset = 0;\n\nif (offset !== 0){\n offset = Math.abs(offset);\n}\n\nmsg.payload = {\n onoffset : offset,\n offoffset : offset\n}\n\nmsg.now = now.getTime();\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":300,"wires":[["d989ed91.8ad29","b0c1d50.9e6ff28"]]},{"id":"d989ed91.8ad29","type":"debug","z":"52175ebb.b4914","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":650,"y":300,"wires":[]},{"id":"f4d3ac93.0baea","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"info","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":200,"wires":[["b0c1d50.9e6ff28"]]},{"id":"31172db9.2cc632","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"off","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":160,"wires":[["b0c1d50.9e6ff28"]]},{"id":"9dcfd8b0.78a708","type":"inject","z":"52175ebb.b4914","name":"","topic":"","payload":"on","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":490,"y":120,"wires":[["b0c1d50.9e6ff28"]]},{"id":"b0c1d50.9e6ff28","type":"schedex","z":"52175ebb.b4914","name":"occupation","suspended":false,"lat":"0","lon":"0","ontime":"06:00","ontopic":"","onpayload":"true","onoffset":0,"onrandomoffset":0,"offtime":"21:30","offtopic":"","offpayload":"false","offoffset":0,"offrandomoffset":0,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"sun":true,"x":770,"y":160,"wires":[["27d32f62.f970c"]]},{"id":"27d32f62.f970c","type":"debug","z":"52175ebb.b4914","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":940,"y":100,"wires":[]}]

```

---

<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:** [16 October 2019 11:05 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/14 "2019-10-16T11:05:50Z")

</div>

Can you post a flow that _doesn't_ do what you expect? To investigate a possible bug it is necessary to have a failing condition.  
I am also in the UK and I just changed the Off time in the schedex node in your flow to 12:03 and let it run without clicking any of the inject nodes and it triggered at 12:03 local time as expected.  
Are you using the latest version of schedex (1.6.2)?

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [17 October 2019 19:07 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/15 "2019-10-17T19:07:10Z")

</div>

Ive looked a bit further and found that the schedex module is outputting a time that is an hour behind, but does actually work when it is set to. Something else occurred unrelated to the schedule, but I thought it was the schedule because it happened an hour earlier than expected.

Im logging a few more things to keep track of everything but thanks for the help.

---

<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:** [17 October 2019 20:19 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/16 "2019-10-17T20:19:53Z")

</div>

> [@steven\_191](#):
>
> the schedex module is outputting a time that is an hour behind

I don't understand what you mean by that. Do you mean that when you send it "info" in the payload the information it emits shows the schedule times in GMT? If so then that is exactly what it says it should do in the info tab for the node. So for the example I mentioned above where I have configured it to trigger at 12:03 local time (UTC+1) it shows "Fri, 18 Oct 2019 11:03:00 GMT" which is correct. If you want it to send it in local time then you can use "info\_local" which for that example shows "2019-10-18T12:03:00.000+01:00" which is also correct.

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [19 October 2019 12:57 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/17 "2019-10-19T12:57:19Z")

</div>

I totally missed that. So it is all good, and I see the correct time if I send info\_local.

I jumped to a conclusion about what the issue was and started looking in the wrong place. But anyway, learned a bit more and found the issue elsewhere.

Thanks

---

<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:** [19 October 2019 13:34 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/18 "2019-10-19T13:34:42Z")

</div>

> [@steven\_191](#):
>
> I see the correct time if I send info\_local.

I have to point out that you were seeing the correct time even when you asked for info rather than info\_local. 11:03:00 GMT is exactly the same time as 12:03:00.000+01:00, it is just displayed in a different way.

---

<div class="post-metadata">

**Author:** ![steven\_191](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@steven\_191](https://discourse.nodered.org/u/steven_191)\
**Post date:** [21 October 2019 07:53 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/19 "2019-10-21T07:53:53Z")

</div>

Yes I did realise that eventually. A made the silly mistake of making assumptions before fully understanding what I was looking at so went down the wrong path straight away.

---

<div class="post-metadata">

**Author:** ![achrefnajjar](https://avatars.discourse-cdn.com/v4/letter/a/8e8cbc/32.png) [@achrefnajjar](https://discourse.nodered.org/u/achrefnajjar)\
**Post date:** [7 April 2021 16:24 UTC](https://discourse.nodered.org/t/using-the-time-in-node-red/16761/20 "2021-04-07T16:24:24Z")

</div>

hello my friend can i ask you how did you get time and date to a text node using a function node when working on raspberry pi ? thanks.

[Next page](https://discourse.nodered.org/t/using-the-time-in-node-red/16761.md?page=2)
