# Node "moment", output format "calendar", different results

**URL:** https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697
**Category:** General
**Created:** [6 March 2020 10:24 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697 "2020-03-06T10:24:19Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 10:24 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/1 "2020-03-06T10:24:20Z")

</div>

Hi,

I am using node `moment` to humanize time. I've tried the Output Format `calendar`. It generally works great but for some 20% cases it gives me wrong time. E.g. from the future.

When it works ok, the message looks like  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bded30889080a9083afc12029693b87c96c9623b.png)

but sometimes it gives me  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/f/cff463bbdd5b6cabdf968653e5f6a18031c13176.png)  
(at the bottom is oryginal format from input)

Does anybody else has this problem?

---

<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: [6 March 2020 10:54 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/2 "2020-03-06T10:54:53Z")

</div>

@QbaF Welcome to the forum! Can you show us the settings you have in the Moment node?

Actually, if it is a small flow, the export the flow and attach it to a reply [AFTER reading this thread](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<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: [6 March 2020 11:37 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/3 "2020-03-06T11:37:43Z")

</div>

Is it the moment (humanizer) node you are using or the moment library in your own code?

Also, can you share an example of both the input data that works and one that does not?

You will also need to share the settings in the humanizer node.

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 12:11 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/4 "2020-03-06T12:11:33Z")

</div>

It is `moment` node.

In my first message you have examples of both cases - good and wrong (at this same flow settings). In both you can see:  
message time  
message title  
Output Time from `moment` node  
Input Time for `moment` node

The full flow:

```auto
[{"id":"152d0e5f.0d3222","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"40c17b7d.b30e24","type":"server-state-changed","z":"152d0e5f.0d3222","name":"Ewa Ping change state","server":"f0c730a9.6c722","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"device_tracker.ewa_ping","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"x":120,"y":200,"wires":[["16c9b768.8960d9"]]},{"id":"b62ec91c.96bb78","type":"api-call-service","z":"152d0e5f.0d3222","name":"Wyślij wiadomość Qbie","server":"f0c730a9.6c722","version":1,"debugenabled":false,"service_domain":"notify","service":"mobile_app_sm_g970f","entityId":"","data":"{\"title\":\"{{topic}} {{payload}}\",\"message\":\"Od {{ostatnio_zmieniono}} \\n {{data.new_state.last_changed}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":850,"y":200,"wires":[[]]},{"id":"133739bc.8d6206","type":"moment","z":"152d0e5f.0d3222","name":"","topic":"","input":"data.new_state.last_changed","inputType":"msg","inTz":"ETC/GMT","adjAmount":0,"adjType":"days","adjDir":"add","format":"calendar","locale":"pl_PL","output":"ostatnio_zmieniono","outputType":"msg","outTz":"Europe/Warsaw","x":560,"y":200,"wires":[["b62ec91c.96bb78"]]},{"id":"16c9b768.8960d9","type":"change","z":"152d0e5f.0d3222","name":"Ewa","rules":[{"t":"set","p":"topic","pt":"msg","to":"Ewa","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":200,"wires":[["133739bc.8d6206"]]},{"id":"f0c730a9.6c722","type":"server","z":"","name":"Home Assistant","legacy":true,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

---

<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: [6 March 2020 12:21 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/5 "2020-03-06T12:21:52Z")

</div>

Sorry, that flow doesn't really help since I don't have all of the nodes it requires. That's why I asked for the data & settings.

The other thing to try would be to run a nodejs app with moment to see if the issue is in the momentjs code or whether it is an artifact of the moment node.

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 13:15 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/6 "2020-03-06T13:15:28Z")

</div>

Of course @TotallyInformation

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/301afaf667ad54a9f18a7a949d510d12653c8251.png)

---

<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: [6 March 2020 13:49 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/7 "2020-03-06T13:49:16Z")

</div>

And the input data examples? Just the data, you can capture that by putting a debug node attached to the output from your initial node. Then in the debug sidebar there is a small icon that lets you copy the data.

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 13:54 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/8 "2020-03-06T13:54:51Z")

</div>

Input data strings are bottom lines of first post screenshots:  
`2020-03-04T07:20:12.598450+00:00`  
`2020-03-04T12:14:57.501133+00:00`

Or do you ask for the complete input object?

---

<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: [6 March 2020 14:17 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/9 "2020-03-06T14:17:36Z")

</div>

Stick a `debug` node on the output of the `moment` node so display when the format changes or is it possibel when you send the data to the `api-call-service` node something is happening to the date after that??

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 14:52 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/10 "2020-03-06T14:52:30Z")

</div>

For correct formating is enough or it have to be for wrong one? As I did try to make it by hand and as for now all the answers was ok.

---

<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: [6 March 2020 14:55 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/11 "2020-03-06T14:55:02Z")

</div>

Sorry I don’t understand what you mean

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 15:11 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/13 "2020-03-06T15:11:53Z")

</div>

OK, we got it.

This is what is getting into the `moment` (with marked most important data):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86222325bf21a8f8b7af946d04a2801dbf3b860b.png)

This is what is going out of the `moment`:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/d/dd0eef8db7bb4151dbe806889d49689c12fb7b9c.png)

This is what we have on `call-service` node:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/364415ae7e714eaa15b364c8098e69fd2ec68b19.png)

---

<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: [6 March 2020 17:26 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/14 "2020-03-06T17:26:16Z")

</div>

When you post a screenshot, we cannot copy it into a flow. You need to post as JSON output.

However, I can now finally see what the issue is. There is a bug in MomentJS that I've raised previously and they have refused to fix.

If you specify an input with more than 3 decimal places on the seconds value, MomentJS will misinterpret the timestamp.

Please feel free to raise this with the authors of Moment as it is a pain. I've put a work-around into the moment node but it doesn't seem to handle the format you've provided. If you remove the timezone offset from the end, it should work.

Here is the workaround code. If you are good with Regular Expressions, perhaps you can extend it to cover your case and I will be happy to publish a new version.

```auto
/** Catch & correct input strings in ISO format with seconds >3dp
 * since parseFormat has an unfixed bug (Issue #24)
 * @param {string} inp Input date/time string
 */
function catchDp(inp) {
    // If ISO string w/ seconds >3dp, then process else exit
    if ( inp.match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{4,}/) ) {
        inp = inp.replace(/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3})\d*(.*)/, '$1$2')
    }

    return inp
}

```

Otherwise, you will need to tweak the input values before sending them to the moment node so that the decimal seconds have no more than 3dp.

---

<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: [6 March 2020 17:48 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/15 "2020-03-06T17:48:45Z")

</div>

I've actually reproduced the issue and will take a deeper look in a bit but my lap has been taken over by my three year old grand daughter. 🥰

---

<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: [6 March 2020 19:46 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/16 "2020-03-06T19:46:29Z")

</div>

@QbaF Do you have any control the format of the date coming in to the moment node?

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [6 March 2020 20:32 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/17 "2020-03-06T20:32:33Z")

</div>

@zenofmud  
There is always possibility to control incoming data by changing it. That is what for I used `moment` node;) But what I receive from `state` node it is, as I know, standard "last changed" data format.

@TotallyInformation  
I am not sure is it problem of "more than 3 decimal places" as in both cases (good and wrong) there are more than 3 decimal places.  
Formating values to send them to format in `moment` does seems as a double work.  
Anyway thanks.

---

<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: [6 March 2020 21:39 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/18 "2020-03-06T21:39:29Z")

</div>

> [@QbaF](#):
>
> standard "last changed" data format.

`57.501133` seconds is not standard and most likely completely ridiculous. 0.000003 seconds is three microseconds. Few systems outside specialist high-performance scientific and industrial systems will ever be able to measure to this accuracy. 😀

> [@QbaF](#):
>
> both cases (good and wrong) there are more than 3 decimal places.

You would need to look up the issue I raised, I was quite a while back now and I can't remember the details.

> [@QbaF](#):
>
> Formating values to send them to format in `moment` does seems as a double work.

Indeed, a pain.

I recommend at least trying the data with the shortened seconds to see if that is the problem.

---

<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: [6 March 2020 21:44 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/19 "2020-03-06T21:44:13Z")

</div>

The format of the date/time being passed to the moment node is causing the issue. What you could do is to add another rule to the change node to truncate the data after the period (.)

Add another 'set' and set `msg.data.new_state.last_changed` and use this jsonata expression  
`$substringBefore(data.new_state.last_changed, '.') & '+00:00'`  
See if that works for you

---

<div class="post-metadata">

### Author: ![QbaF](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/qbaf/32/51164_2.png) [@QbaF](https://discourse.nodered.org/u/QbaF)
#### Post date: [7 March 2020 16:07 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/20 "2020-03-07T16:07:26Z")

</div>

@TotallyInformation

> `57.501133` seconds is not standard and most likely completely ridiculous.

I am not sure that all nodes, even most has this format for last changed / last updated but at least a lot does. Isn't `moment` repairable?  
But can not disagree about the "ridiculous"

Could You please send link to the issue mentioned. [I can see only two issues](https://github.com/TotallyInformation/node-red-contrib-moment/issues/28) connected with `moment` but no one is yours.

@zenofmud  
Thank you. I will get it a try.  
But maybe for future use need to find and try better working node.

---

<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: [7 March 2020 16:20 UTC](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697/21 "2020-03-07T16:20:59Z")

</div>

> [@QbaF](#):
>
> Could You please send link to the issue mentioned. [I can see only two issues](https://github.com/TotallyInformation/node-red-contrib-moment/issues/28) connected with `moment` but no one is yours.

I assume you ment this for me. When I said I reproduced the issue I ment what I said. I caused it to happen in my node-red. Somethng about the incoming date is causing an issue with the moment node. Since you don't seem to be able to control the format of the date being sent to your flow, I created a 'get arround' for you. I have not opened an issue in the moment node because the date format is not what I would call standard.

UPDATE: you may want to go [read this post about the moment node](https://discourse.nodered.org/t/need-help-with-a-sub-flow-and-dates-sorry-folks/22740/19)

[Next page](https://discourse.nodered.org/t/node-moment-output-format-calendar-different-results/22697.md?page=2)
