I thought I understand how datetime works in Javascript, but I sometimes I run into issues and I question myself over-and-over again. I know that the timestamp is stored in UTC and we should only do a conversion to local timezone when we want to print the information on the screen. And I know the timezone on the machine where Node-Red is running should be set correctly according to the local timezone.
I am working on machine in Finland, Node-Red is running on an IoT gateway also in Findland which is configured like this:
I assume this is all correct, since Finland in UTC+3 and GMT+2 because of the DST now.
If I do a simple toLocaleString
it just converts the timestamp into a time format, in this case UK but the time is the UTC time:
The local time is 23:50 as shown currectly first header of the debug message, but I assume this only validates that the browser is running in the correct local timezone.
Now if I print the getTimezoneOffset
, I get -180 which is the UTC offset in the Summer (DST active).
That again is correct, so it means that Node-Red knows that the local timezone is Finland.
I created this simple function to convert the UTC time to Finnish local time:
This code works, as I get the time in local time, and matches the time in the computer I work on. Everything looks good.
Did I do something fundamentally wrong? Will this code work once the DST goes away?
To be honest I still don't understand why I had to offset my timestamp the the value of getTimezoneOffset
but also the additional 2 hours in tzOffset
. I thought it should be able to create a code which always works assuming the timezone on the machine which Node-Red runs on is set up correctly.
Sorry if this has been discussed before, but the other topics I looked were caused by the incorrect timezone setting on the PI, which as I understand is not the case here.