# Timestamp conversion going wrong

**URL:** <https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110>\
**Category:** General\
**Created:** [16 February 2021 18:08 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110 "2021-02-16T18:08:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![haskle](https://avatars.discourse-cdn.com/v4/letter/h/e19adc/32.png) [@haskle](https://discourse.nodered.org/u/haskle)\
**Post date:** [16 February 2021 18:08 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/1 "2021-02-16T18:08:13Z")

</div>

I want to convert a json(?) timestamp from, eg, `1613498523` to "human readable"; I have tried using the **moment** node, but whatever output format I choose, the date is right but the time is always `00:00` (or `00:00:00`) - which is not what an online converter interprets the initial input as.

Where am I going wrong?

TIA

![Screenshot 2021-02-16 at 18.04.49](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/d/0ddb701c7deb4f6f952440e0530a2b49643afbd7.png)

.  
.

 ![Screenshot 2021-02-16 at 18.05.13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/975f140e8a6252ebe307030f1aab83c6f288d3a8.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [16 February 2021 18:39 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/2 "2021-02-16T18:39:14Z")

</div>

Try entering `payload.ts` in the top msg input field. The node needs the timestamp. As you did not feed it one it gave you the output for epoch which is midnight jan 1st 1970 so you get 00:00:00

You can also do this in the change node.

```auto
[{"id":"52919353.50ca94","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":2560,"wires":[["ee7bd564.7ecb58"]]},{"id":"ee7bd564.7ecb58","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload).tz(\"London/Europe\").format(\"LTS\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2560,"wires":[["596cd523.90c89c"]]},{"id":"596cd523.90c89c","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":2440,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [17 February 2021 00:10 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/3 "2021-02-17T00:10:16Z")

</div>

Depending on what you mean by "human readable", you can also simply use a function node:

```auto
const ts = new Date(msg.payload.ts)
return {
  payload: ts.toISOString()
}

```

There are a few other native methods as well:

[Date.prototype.toISOString() - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString#)

---

<div class="post-metadata">

**Author:** ![haskle](https://avatars.discourse-cdn.com/v4/letter/h/e19adc/32.png) [@haskle](https://discourse.nodered.org/u/haskle)\
**Post date:** [18 February 2021 12:45 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/4 "2021-02-18T12:45:29Z")

</div>

Many thanks @E1cid & @TotallyInformation ; I have made some progress.

Using the `node-red-contrib-moment` Date/Time editor node I am able to change the object ts: string to a payload of, eg, "18 Feb 2021 12:32".

But the problem I now have is that when this arrives at my MQTT broker it seems to think it is a value pair(?), and each message creates a new Input, eg input name = `18 Feb 2021 12` with a value of `32`.

How might I change the message to be Input name `emon/mqtt_time` and a value of the full time, ie `18 Feb 2021 12:32`?

 ![Screenshot 2021-02-18 at 12.33.05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/4/9494a7ab2f0698805cdc6f087f172f1086033891.png)

---

<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 February 2021 14:05 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/5 "2021-02-18T14:05:56Z")

</div>

I don't fully follow what you are saying. Are you sending it to an MQTT broker? If so then you should send it as an ISO string and convert it to a readable form at the other end.

> [@haskle](#):
>
> How might I change the message to be Input name `emon/mqtt_time` and a value of the full time, ie `18 Feb 2021 12:32` ?

Is that not exactly what the second output in your debug screenshot shows? If that isn't what you want please explain again, showing what input you are getting and what output you want.

---

<div class="post-metadata">

**Author:** ![haskle](https://avatars.discourse-cdn.com/v4/letter/h/e19adc/32.png) [@haskle](https://discourse.nodered.org/u/haskle)\
**Post date:** [19 February 2021 11:12 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/6 "2021-02-19T11:12:17Z")

</div>

> [@Colin](#):
>
> I don't fully follow what you are saying.

Not sure I do either TBH 😀 But to summarise:

- My Weather Station sends out data via a url which includes a timestamp in the format `ts: 1613651565`. I would like to make this human readable within a Home Assistant dashboard so that I can quickly see how "old" the last update was, and very preferably more than just the ISO8601 format, as that isn't very easy to scan read.
- My plan was to send this info to my MQTT broker on another device on my LAN, and from there in to my Home Assistant.
- I have a NR function node that extracts this info:

```auto
msg.payload = {
"ts":msg.payload.data.ts};
return msg;

```

- It then passes on to a `moment` Date/Time Formatter node, but it is here that I am getting a bit stuck as even though it sends on the ISO8601 data, by the time it arrives in my MQTT broker it sees a value pair(?) and gives the Input name as eg `2021-02-18T16` and a value of `49` (due to the colon) which is not a lot of use 😅

Many thanks,

 ![Screenshot 2021-02-19 at 11.09.40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d7987d1772878bc30dd50e9f7ce63d4e70e24f4.png)

---

<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:** [19 February 2021 11:42 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/7 "2021-02-19T11:42:16Z")

</div>

Can't HA convert from a millisecond timestamp to human readable? It is generally best to do that sort of conversion at the point of display if possible.

---

<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:** [20 April 2021 11:43 UTC](https://discourse.nodered.org/t/timestamp-conversion-going-wrong/41110/8 "2021-04-20T11:43:01Z")

</div>

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