Problem with moment

Please have a look at the attached flow.
Why does moment in a moment node deliver a different result as moment in change node?

[{"id":"8d803f5dd58c9965","type":"debug","z":"db81a52a5a9665c2","name":"debug 2458","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":1100,"wires":[]},{"id":"480ecf6bab01aea5","type":"change","z":"db81a52a5a9665c2","name":"timestamp","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(msg.payload).tz(\"Europe/Berlin\").format(\"x\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":1040,"wires":[["8d803f5dd58c9965","6858c4f9e723dd6c","d4c0997082ea3cdd"]]},{"id":"c2cb935566602f4c","type":"inject","z":"db81a52a5a9665c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1700926319561","payloadType":"num","x":300,"y":1040,"wires":[["96eb10de94187b96"]]},{"id":"96eb10de94187b96","type":"change","z":"db81a52a5a9665c2","name":"HH:mm:ss DD-MM-YYYY","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload).tz(\"Europe/Berlin\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":1040,"wires":[["480ecf6bab01aea5","6a4c5b0b1a2daa20"]]},{"id":"6a4c5b0b1a2daa20","type":"debug","z":"db81a52a5a9665c2","name":"debug 2459","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":1100,"wires":[]},{"id":"6858c4f9e723dd6c","type":"change","z":"db81a52a5a9665c2","name":"YYYY-MM-DD HH:mm:ss","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(msg.payload).tz(\"Europe/Berlin\").format(\"YYYY-MM-DD HH:mm:ss\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1090,"y":1100,"wires":[["90d7399b218087cf"]]},{"id":"90d7399b218087cf","type":"debug","z":"db81a52a5a9665c2","name":"debug 2460","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1110,"y":1140,"wires":[]},{"id":"d4c0997082ea3cdd","type":"moment","z":"db81a52a5a9665c2","name":"","topic":"","input":"","inputType":"msg","inTz":"Europe/Berlin","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"de-DE","output":"","outputType":"msg","outTz":"Europe/Berlin","x":1080,"y":980,"wires":[["d77fe04c051c9f6d"]]},{"id":"d77fe04c051c9f6d","type":"debug","z":"db81a52a5a9665c2","name":"debug 2461","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1090,"y":1020,"wires":[]}]

Hi, perhaps you could show us rather than having to install a contrib node and add a flow to our systems?

I don't use moment, neither have I looked at the flow in Node RED...
But, I note from your JSON, that you set the locale to "de-DE" and at the same time set the TZ to "Europe/Berlin"

So I wonder, if the node is not adjusting the time, given the input timestamp is UTC, therefore its not adjusting because it thinks the input is already at the target TZ via the locale value?

where as a change Node, does not take what locale the timestamp is for, therefore treats it as UTC.

just a guess, but I don't use moment, so could be wrong :man_shrugging:

Since I have moment node installed already, pasting screenshot of the flow with debug outputs for expert's comments.
I have added comment nodes above each change nodes to denote whats inside.

moment node config

Might have something to do with the timestamp from change node called timestamp outputting a string timestamp
i.e
"1611234567890"
rather than a number
i.e
1621234567890

1 Like

yes, that is the problem.

This is the way to do it: $moment(msg.payload).tz("Europe/Berlin").format(x)

and not:$moment(msg.payload).tz("Europe/Berlin").format("x")

after changing "x" t0 x

image

Not quite
$moment(msg.payload).tz("Europe/Berlin").format(x)
x would be taken as a msg.x, which is undefined so would be the same as
$moment(msg.payload).tz("Europe/Berlin").format() which would return a ISO timestamp as @smanjunath211 image shows in the debug.
The monent(input), input would need to be a timesatmp in milliseconds (not a string). or an ISO timestamp string or time object.

In my working Flow I had to use $moment(msg.payload).tz("Europe/Berlin").format("x")
in the change node feeding the string result into a node with parseInt(msg.payload) in order to get a integer timestamp.

with one node to timestamp:

[{"id":"ce5ffb82819dcc99","type":"inject","z":"db81a52a5a9665c2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":1360,"wires":[["ec28f244083341fd"]]},{"id":"ef3618202da4a3e8","type":"function","z":"db81a52a5a9665c2","name":"to timestamp","func":"return { payload: parseInt(moment(msg.payload).tz(\"Europe/Berlin\").format(\"x\"))}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[{"var":"moment","module":"moment"},{"var":"momentTimezone","module":"moment-timezone"}],"x":710,"y":1360,"wires":[["a0135cbc99f10e25"]]},{"id":"a0135cbc99f10e25","type":"debug","z":"db81a52a5a9665c2","name":"debug 2464","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":870,"y":1360,"wires":[]},{"id":"ec28f244083341fd","type":"change","z":"db81a52a5a9665c2","name":"HH:mm:ss DD-MM-YYYY","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(payload).tz(\"Europe/Berlin\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":1360,"wires":[["ef3618202da4a3e8"]]}]

You input a timestamp (which is utc, no timezome)
then use moment to give timezone and format to milliseconds ( which is utc, no time zone)
The result is exactly the same timestamp.

Please explain what you are attempting to do, as it seems you are doing nothing of any value.

example of what i see you doing

[{"id":"ce5ffb82819dcc99","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":1540,"wires":[["ec28f244083341fd","a0135cbc99f10e25"]]},{"id":"a0135cbc99f10e25","type":"debug","z":"d1395164b4eec73e","name":"debug 2464","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":370,"y":1640,"wires":[]},{"id":"ec28f244083341fd","type":"change","z":"d1395164b4eec73e","name":"HH:mm:ss DD-MM-YYYY","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number($moment(payload).tz(\"Europe/Berlin\").format(\"x\"))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":1480,"wires":[["a0135cbc99f10e25"]]}]

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