Need help with converting a Timestring to a readable format

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.

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.

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

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

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

[{"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":[]}]

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

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.

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

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

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.

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

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