Gawan
27 March 2023 08:59
1
Hi,
I just found out, that my NR is showing the wrong timezone.
Although my server is using CEST and the right time ...
... if I use this function in NR
msg.payload = new Date().toISOString();
I get this as a result:
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
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)
Colin
27 March 2023 09:28
5
What do you want to do with the time? The time in the debug node is correct.
Gawan
27 March 2023 09:31
6
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
Colin
27 March 2023 09:33
7
Have are you displaying it in the graph?
jbudd
27 March 2023 09:45
8
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.
Gawan
27 March 2023 09:57
10
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" ?
jbudd
27 March 2023 10:04
11
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.
E1cid
27 March 2023 10:04
12
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
Colin
27 March 2023 10:29
14
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.
Gawan
27 March 2023 13:09
15
I just tried all three of them, but result is always the same .... wrong
Colin
27 March 2023 13:46
16
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.
jbudd
27 March 2023 23:00
18
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 ?
Gawan
28 March 2023 05:14
19
I am using 1 server with 20 node-red instances in a docker containers
the server as such is running on CEST
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 ?
Colin
28 March 2023 06:01
20
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.