How to create a timestamp in the right format for a dashboard chart

The question may sounds dumb, I am an absolute beginner.
So I want to store data in a csv file, and later on I want to display this data through a chart. I already tried to make a timestamp with msg.payload.timestamp = new Date() but that did not work with the chart node.
In the example from the chart node the timestamps are just numbers but I do not know how to create a timestamp that looks like that.

[{
"series": ["A", "B", "C"],
"data": [
    [{ "x": 1504029632890, "y": 5 },
     { "x": 1504029636001, "y": 4 },
     { "x": 1504029638656, "y": 2 }
    ],
    [{ "x": 1504029633514, "y": 6 },
     { "x": 1504029636622, "y": 7 },
     { "x": 1504029639539, "y": 6 }
    ],
    [{ "x": 1504029634400, "y": 7 },
     { "x": 1504029637959, "y": 7 },
     { "x": 1504029640317, "y": 7 }
    ]
],
"labels": [""]
}]

I hope the question does not sound to stupid, I have just started with nod-red.

1 Like

There is a great example flow you should import with lots of chart examples:
https://flows.nodered.org/flow/2938d98507c81648a2ab5ac1ba6e7d31
give it a try and take a look at the examples and see if one of them answers your question (or generates more questions :grinning_face_with_smiling_eyes:)

That x number is a JS internal timestamp -- it's the number of milliseconds since the epoch on 1/1/1970 00:00:00 GMT

The chart node inherently understands it as a timestamp and converts it to the local timezone for display. In JS, you can convert that to a human-readable Date object like so:

> new Date(1504029632890)
2017-08-29T18:00:32.890Z

> new Date(1504029632890).toString()
'Tue Aug 29 2017 14:00:32 GMT-0400 (Eastern Daylight Time)'

The first example is called ISO format, and is suitable for charts, as well as most databases. It is the only reliable format that should be used for internal datetime storage since it is not open to mis-interpretation. If you decide to store local timestamps, you will have problems reliably converting things like timezone offsets, daylight savings adjustments, and especially doing date arithmetic like time between 2 dates.

If you have a datetime string, many formats are recognized and can be parsed into an internal timestamp, like so:

> Date.parse('29-Aug-2017 2:00:32 pm')
1504029632000
> Date.parse('29-Aug-2017 2:00:32 pm EDT')
1504029632000
> Date.parse('29-Aug-2017 2:00:32 pm GMT')
1504015232000

Just be aware that the local timezone will be used if not given (notice the first 2 are the same for me in the Eastern US, while the 3rd uses Greenwich Mean Time).

1 Like

Thanks for the answer. In the end I usednode-red-contrib-simpletime to create the timestamp

The inject node, by default, creates a timestamp.

In a function node you would do...

msg.payload = { 
  timestamp: Date.now()
}
return msg;

Then there is JSONata in the change node and a few other ways too. None that require a contrib node to be installed.

Thanks Date.now() gives me the right format. This was what I was looking for. Thanks a lot!!

When I put this timestamp into my chart it displays the time but its 1 hour off. it says 16:30 but in my country its 17:30, what can i do to fix this?

Are the node-red server and the computer where the UI is being viewed in the browser set for same region and timezone?

Okay I fixed it, I just added 3600000 to the timestamp. The problem was that the chart node displays the time in GMT, i dont know how I can change this. There is probably a more elegant way to deal with this problem than just adding 3600000 milliseconds to the timestamp.

Yeah the node red server is running on my computer and I am accessing the ui on the same computer. Thats why I am confused why it shows the time in GMT

Are you going to the chart by reading the CSV file and displaying it on the chart? If so then show us how you are generating the timestamp to send to the chart, and show us what the file looks like.

While I was writing my answer I saw that in the chart node a checkbox with "show UTC" was activated. I just did not see it. So it does work, not anymore help needed. Still, thanks for your helpfulness!

1 Like

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