# Determine the local time in function node

**URL:** <https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502>\
**Category:** General\
**Created:** [21 April 2021 12:45 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502 "2021-04-21T12:45:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [21 April 2021 12:45 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/1 "2021-04-21T12:45:14Z")

</div>

I have a few scenarios where I need to get the current time in a function node for example to generate a filename based on the current timestamp. I don't want to use an external node as it would complicate the flow, and I am working in an environment where I cannot install other node.js component (like moment as a global object).

I have done this code:

```auto
// Get the current time and convert it to text
var now = new Date();

let localDate = new Date(now.toLocaleString('en-US', { timeZone: "Europe/Budapest" }));
let offset = now.getTime() - localDate.getTime();
now.setTime( now.getTime() + offset );

var yyyy = now.getFullYear();
var mm = now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : (now.getMonth() + 1); // getMonth() is zero-based
var dd = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
var hh = now.getHours() < 10 ? "0" + now.getHours() : now.getHours();
var mmm = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();
var ss = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();

node.status({fill:"grey",shape:"dot",text:yyyy+"."+mm+"."+dd+" "+hh+":"+mmm+":"+ss});

return msg;

```

This gets the current time (which is in UTC), offsets it with a given timezone and slices out the various year, month, day, hour, minute, second etc.

First of all, do you see any issues with the above code? It works on my pi running Node-Red.

Second question: I was reusing this logic in Node-Red running on a Wiedmüller AC PLC. Actually on two different machines. Both of them are set up to use a NTP server, and the PLC clock was set correctly (with the correct time date talking into account the current summer time period). Still on one Node-Red, the very same code procures the correct time, on the other Node-Red, the time is 4 hours behind. I realized it is 4 hours, because I am 2 hours ahead of UTC (in CET DST) and 4 hours is 2 hours added the wrong way. So I fixed it by changing "now.getTime() + offset" to "now.getTime() - offset". But I don't understand why? What makes the two Node-Red instance different? I am pretty sure this is related to how JS handles time and date, but can't figure it out.

Thanks,  
Csongor

---

<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:** [21 April 2021 13:00 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/2 "2021-04-21T13:00:15Z")

</div>

I'm confused (easily done!). Why would you want to get the time, convert it, find the offset, difference it and add the offset?

`new Date()` gives you the time in the OS's timezone. If you can set that to local, it will give you the right time anyway. Of course, I don't generally recommend that since it is usually easier to keep everything in UTC as long as possible.

But as long as you are using an up-to-date version of Node.js, you can probably use `Intl` to get what you want anyway.

[Intl.DateTimeFormat() constructor - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat)

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [21 April 2021 13:33 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/3 "2021-04-21T13:33:20Z")

</div>

> [@TotallyInformation](#):
>
> O

OK, I am getting confused as well:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/811f2a002f9d49d5d450de526f38306f0838092c.png)  
Everything looks fine now.  
When I did the same code back in November, I had to add 1 hour manually, like it was not taking the winter time into account. On the very same raspberry PI. So this makes me even more confused.

And still does not explain why the two computers are not behaving the same. Although I need to check the how the time zones are set up on both.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 April 2021 13:50 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/4 "2021-04-21T13:50:31Z")

</div>

CEST has a daylight savings time which changes at end of october and march, This may have been why you saw the hour difference.

---

<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:** [21 April 2021 13:52 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/5 "2021-04-21T13:52:13Z")

</div>

Can you run `timedatectl` on both devices and report the outputs?

```bash
home@home:~$ timedatectl
               Local time: Wed 2021-04-21 14:51:07 BST
           Universal time: Wed 2021-04-21 13:51:07 UTC
                 RTC time: Wed 2021-04-21 13:51:06
                Time zone: Europe/London (BST, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
home@home:~$

```

---

<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:** [21 April 2021 14:09 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/6 "2021-04-21T14:09:44Z")

</div>

> [@nygma2004](#):
>
> `toLocaleString(`

If you are using a different version of nodejs that may explain something. There were problems with `toLocaleString()` in some earlier versions.

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [21 April 2021 14:19 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/7 "2021-04-21T14:19:36Z")

</div>

On the PLC, there is no command prompt, or console that I can run commands.  
My system looks good. Still, I don't understand why I was off by one hour in the winter.

```auto
pi@OpenHAB:~ $ timedatectl
      Local time: Wed 2021-04-21 16:17:03 CEST
  Universal time: Wed 2021-04-21 14:17:03 UTC
        RTC time: n/a
       Time zone: Europe/Budapest (CEST, +0200)
     NTP enabled: no
NTP synchronized: yes
 RTC in local TZ: no
      DST active: yes
 Last DST change: DST began at
                  Sun 2021-03-28 01:59:59 CET
                  Sun 2021-03-28 03:00:00 CEST
 Next DST change: DST ends (the clock jumps one hour backwards) at
                  Sun 2021-10-31 02:59:59 CEST
                  Sun 2021-10-31 02:00:00 CET

```

---

<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:** [21 April 2021 14:29 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/8 "2021-04-21T14:29:28Z")

</div>

> [@nygma2004](#):
>
> `OpenHAB`

Is that playing a part?

Also, why is NTP sync'd but not enabled? Maybe `timesyncd.service` isn't running?

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [24 April 2021 17:45 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/9 "2021-04-24T17:45:01Z")

</div>

I had this PI original for OpenHAB, but I migrated to Node-Red even removed OpenHAB, so that should not play any part. I was not aware of the timesyncd service, but I have restarted it now.

---

<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:** [23 June 2021 17:45 UTC](https://discourse.nodered.org/t/determine-the-local-time-in-function-node/44502/10 "2021-06-23T17:45:58Z")

</div>

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