Search code examples
javascriptraspberry-pitimezonenode-redtimezone-offset

Javascript Date.getTimezoneOffset() not working in Node-red


I have Node-red running on a Raspberry Pi 3B with Raspbian Stretch.
This output confirms that the CET timezone is actually set:

pi@raspberrypi:~ $ timedatectl
      Local time: Tue 2019-12-10 22:29:51 CET
  Universal time: Tue 2019-12-10 21:29:51 UTC
        RTC time: n/a
       Time zone: Europe/Berlin (CET, +0100)
 Network time on: yes
NTP synchronized: yes
 RTC in local TZ: no

I am trying to calculate the timezone-offset between my current timezone (CET) and the GMT time.
To this end I am using the following line in the function node of node-red:

msg.payload = new Date().getTimezoneOffset();

But instead of an expected offset of one hour (60 minutes), it returns 0.


Solution

  • According to this and this, one needs to set the time zone explicitly for Node-Red in the settings.js file (located in $home/.node-red):

    process.env.TZ = "Europe/Berlin";