Determine the local time in function node

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:

// 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

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)

OK, I am getting confused as well:


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.

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

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

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:~$

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

1 Like

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.

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

Is that playing a part?

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

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.

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