Wrong Timezone in NR - 2 hours to early

Hi,

I just found out, that my NR is showing the wrong timezone.

Although my server is using CEST and the right time ...
image

... if I use this function in NR

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

I get this as a result:

image

Unfortunately it is 2 hours too early and I have no idea where it comes from.

Any ideas ?

BR
G

Hi @Gawan

That is correct toISOString() will report in UTC.
CEST is +2 hours

what else should I use ?

You can use a change node, to set the payload to below.
this will retain the offset I believe

Use the expression type in the dropdown

$moment().toISOString(true)

image

What do you want to do with the time? The time in the debug node is correct.

I want to show the time in a graph and people are complaining if it says "08:50" when it is "10:50" in reality :slight_smile:

Have are you displaying it in the graph?

Interestingly, the time format in the debug panel depends on debug node settings.

Are these people in the same timezone as the Node-red machine, ie CEST?

If you wanted to do this in a function node only.
This will increase your localtime by 2 hours, before ISOString() reduces it according to the offset

const currentTime = new Date().getTime();
const date = new Date(currentTime + 2 * 60 * 60 * 1000);
msg.payload = date.toISOString();
return msg

there are also moment nodes one can install.

Yes, all people are sitting in the same timeszone as the server - CEST

I do not want to add static 2 hours to the timestamp - I want the timestamp to adapt to daylight savings.

Isn't there any functionality showing the right "Local time" ?

I think you need to show us a screen capture of the graph (showing the 2 hour problem) and an export of the graph UI node & nodes that feed it data points.

Set the chart to UTC.
then it will display your UTC ISO timestamps


Data

[{"data":[[{"x":"2023-03-27T10:50:40.000Z","y":1},{"x":"2023-03-27T11:50:40.000Z","y":2},{"x":"2023-03-27T12:50:40.000Z","y":1},{"x":"2023-03-27T13:50:40.000Z","y":2}]],"series":["a"]}]

Hi @Gawan

As noted above, the function toISOString() always returns the time in UTC.

If you want it in the local time you can use: toLocaleString() or just toString()

Lots of details of the options available in the MDN docs: Date.prototype.toLocaleString() - JavaScript | MDN

You need to tell us what sort of chart it is. If it is the dashboard chart it should automatically show the time in the time zone of the machine running the browser.
If it isn't a dashboard chart what is it and how to you give it the timestamps.

I just tried all three of them, but result is always the same .... wrong

image

Are you running node red in Docker or HA or something else other than a native install

Try

const date = new Date('2023-03-27T10:50:40.000Z')

const options = {
    year: "numeric",
    month: "numeric",
    day: "numeric",
    hour: "numeric",
    minute: "numeric",
    second: "numeric",
    hour12: false,
    timeZone: "Europe/Warsaw",
}

msg.payload = Intl.DateTimeFormat('default', options).format(date)

The time zone was a CEST time zone chosen at random so set to whatever it should be.

I think this demonstrates that the timezone setting on your Node-red server is GMT, even though your date command shows CEST?

What does the timedatectl command show?
What about ls -l /etc/localtime ?

I am using 1 server with 20 node-red instances in a docker containers

the server as such is running on CEST
image

Timezone is Europe/Vienna

But I do not know if the container files are using different timezones - I do not know how to verify / change this ?
It it even possible ?

Almost certainly that is the problem. You need to set the container to use the host timezone. I don't use docker but I am sure that a search engine will tell you how.