# Problem with moment

**URL:** <https://discourse.nodered.org/t/problem-with-moment/83182>\
**Category:** General\
**Created:** [25 November 2023 12:31 UTC](https://discourse.nodered.org/t/problem-with-moment/83182 "2023-11-25T12:31:10Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 November 2023 12:31 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/1 "2023-11-25T12:31:10Z")

</div>

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

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

```

---

<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:** [25 November 2023 12:36 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/2 "2023-11-25T12:36:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [25 November 2023 13:13 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/3 "2023-11-25T13:13:09Z")

</div>

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 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [25 November 2023 13:44 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/4 "2023-11-25T13:44:25Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ede5bf9d73df988466a0c0f32ae69ce2ba11d893.png)

moment node config

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/2/321fcfb18f2fd7a84c77aff0a17dce6d54ab14f1.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:** [25 November 2023 14:18 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/5 "2023-11-25T14:18:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 November 2023 15:07 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/6 "2023-11-25T15:07:22Z")

</div>

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")

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [25 November 2023 15:09 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/7 "2023-11-25T15:09:30Z")

</div>

after changing "x" t0 x

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2cef51344adcf7e72939c3729183e6f648fecdbb.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:** [25 November 2023 15:19 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/8 "2023-11-25T15:19:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 November 2023 15:57 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/9 "2023-11-25T15:57:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [25 November 2023 16:27 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/10 "2023-11-25T16:27:06Z")

</div>

with one node to timestamp:

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

```

---

<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:** [25 November 2023 16:49 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/11 "2023-11-25T16:49:45Z")

</div>

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

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

```

---

<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 January 2024 16:49 UTC](https://discourse.nodered.org/t/problem-with-moment/83182/12 "2024-01-24T16:49:52Z")

</div>

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