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

**URL:** <https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [16 January 2022 14:39 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749 "2022-01-16T14:39:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [16 January 2022 14:39 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/1 "2022-01-16T14:39:12Z")

</div>

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.

```auto
[{
"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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 January 2022 14:45 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/2 "2022-01-16T14:45:50Z")

</div>

There is a great example flow you should import with lots of chart examples:  
[https://flows.nodered.org/flow/2938d98507c81648a2ab5ac1ba6e7d31](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 😄)

---

<div class="post-metadata">

**Author:** ![shrickus](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/shrickus/32/517_2.png) [@shrickus](https://discourse.nodered.org/u/shrickus)\
**Post date:** [17 January 2022 03:34 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/3 "2022-01-17T03:34:29Z")

</div>

> [@nodenoob](#):
>
> `{ "x": 1504029632890, "y": 5 }`

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:

```auto
> 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.

> [@nodenoob](#):
>
> I do not know how to create a timestamp that looks like that

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

```auto
> 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).

---

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [17 January 2022 12:51 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/4 "2022-01-17T12:51:27Z")

</div>

Thanks for the answer. In the end I used[node-red-contrib-simpletime](https://flows.nodered.org/node/node-red-contrib-simpletime) to create the timestamp

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 January 2022 13:22 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/5 "2022-01-17T13:22:23Z")

</div>

> [@nodenoob](#):
>
> 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

The inject node, by default, creates a timestamp.

In a function node you would do...

```auto
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.

---

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [17 January 2022 16:14 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/6 "2022-01-17T16:14:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [17 January 2022 16:43 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/7 "2022-01-17T16:43:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 January 2022 16:58 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/8 "2022-01-17T16:58:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [17 January 2022 16:59 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/9 "2022-01-17T16:59:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [17 January 2022 17:01 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/10 "2022-01-17T17:01:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [18 January 2022 08:58 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/11 "2022-01-18T08:58:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nodenoob](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nodenoob](https://discourse.nodered.org/u/nodenoob)\
**Post date:** [18 January 2022 12:56 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/12 "2022-01-18T12:56:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 February 2022 12:57 UTC](https://discourse.nodered.org/t/how-to-create-a-timestamp-in-the-right-format-for-a-dashboard-chart/56749/13 "2022-02-01T12:57:03Z")

</div>

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