# Need help with converting a Timestring to a readable format

**URL:** https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732
**Category:** General
**Created:** [10 June 2022 14:35 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732 "2022-06-10T14:35:39Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Marv21](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Marv21](https://discourse.nodered.org/u/Marv21)
#### Post date: [10 June 2022 14:35 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/1 "2022-06-10T14:35:39Z")

</div>

Hello,  
it seemed like an easy Task but i cant get it to work.  
This are my first footsteps with js or Nodered so be nice, please ^^.

Iam getting a Timestring vom a thirdparty tool over MQTT.

```auto
10.6.2022, 16:23:35[node: Time]
msg.payload : string[14]

"20220610142332"

```

This should be UTC Time, but how can i convert it to my local Time? or at least to something better readable?.

I tried the moment node with YYYYMMDDHHmmss but that doesnt work.

```auto
"The input property was NOT a recognisable date. Output will be a blank string"

```

Can someone push me into the right direction please...

---

<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: [10 June 2022 15:10 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/2 "2022-06-10T15:10:56Z")

</div>

You could use moment js in a change node or a function node. Or the moment contrib node.  
e.g.

```auto
[{"id":"1e29348.e10cecc","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20220610142332","payloadType":"str","x":180,"y":3200,"wires":[["8467d2b6.b724d8"]]},{"id":"8467d2b6.b724d8","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment($$.payload, \"YYYYMMDDHHmmss\").tz(\"Europe/Lisbon\").format(\"yyyy/MM/DD HH:mm:ss\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":3240,"wires":[["81fee731.55f708"]]},{"id":"81fee731.55f708","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":3160,"wires":[]}]

```

---

<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: [10 June 2022 16:15 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/3 "2022-06-10T16:15:10Z")

</div>

> [@Marv21](#):
>
> `"20220610142332"`

That looks like a string which represent 2022/06/10 14:23:32

---

<div class="post-metadata">

### Author: ![Marv21](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Marv21](https://discourse.nodered.org/u/Marv21)
#### Post date: [10 June 2022 19:36 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/4 "2022-06-10T19:36:51Z")

</div>

This works very well, except that the time is 2h behind. I set Berlin as the Location. I think i need to find out how to set moment to recognize my string as UTC.

---

<div class="post-metadata">

### Author: ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)
#### Post date: [10 June 2022 20:49 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/6 "2022-06-10T20:49:01Z")

</div>

Normally if you user format with the moment - it will be converted to local time. Otherwise specify .utc(true) after your moment object.

---

<div class="post-metadata">

### Author: ![Marv21](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Marv21](https://discourse.nodered.org/u/Marv21)
#### Post date: [10 June 2022 21:03 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/7 "2022-06-10T21:03:02Z")

</div>

thank you !  
Tried +0000 and moment.utc etc.  
nothing works. but with .utc(true) works fine.  
THANKS!

---

<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: [10 June 2022 21:40 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/8 "2022-06-10T21:40:21Z")

</div>

Works for me tz("Europe/Berlin"). Output 15:23:32, Which is CEST.  
[edit]

> By default, moment parses and displays in local time.

On further looking I see it actually outputs at local time, so when 14:12 is CEST(your node-red device local i believe), utc would be 12:12.  
here is a hacky way to get utc and convert to CEST.

```auto
$moment(
   $moment($$.payload, "YYYYMMDDHHmmss").format("yyyy-MM-DDTHH:mm:ss") &
   ".000Z"
).tz("Europe/Berlin").format("yyyy/MM/DD HH:mm:ss")

```

---

<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: [24 June 2022 21:41 UTC](https://discourse.nodered.org/t/need-help-with-converting-a-timestring-to-a-readable-format/63732/9 "2022-06-24T21:41:06Z")

</div>

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