# Problem comparing dates

**URL:** <https://discourse.nodered.org/t/problem-comparing-dates/86054>\
**Category:** General\
**Created:** [1 March 2024 13:33 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054 "2024-03-01T13:33:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)\
**Post date:** [1 March 2024 13:33 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/1 "2024-03-01T13:33:04Z")

</div>

I'm trying to pull a date from an online database and compare it with today's date. I can get the data ok but for some reason in the function node the date format is being changed. I'm in the UK and use dd/mm/yyyy but for some reason its being changed to mm/dd/yyyy and doing the comparison. I can't find out where I have gone wrong.

```auto
[{"id":"740c0c0471c21759","type":"tab","label":"Flow 5","disabled":false,"info":"","env":[]},{"id":"1eedac72d8918492","type":"inject","z":"740c0c0471c21759","name":"Test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":90,"y":240,"wires":[["99f8b2657ee7f1f5","6e7bd3e5b29aaf66"]]},{"id":"6e7bd3e5b29aaf66","type":"moment","z":"740c0c0471c21759","name":"Current date","topic":"","input":"payload","inputType":"msg","inTz":"Europe/London","adjAmount":"0","adjType":"hours","adjDir":"subtract","format":"L","locale":"","output":"currentdate","outputType":"flow","outTz":"Europe/London","x":250,"y":300,"wires":[["d12a0c1ac4222f47"]]},{"id":"d12a0c1ac4222f47","type":"debug","z":"740c0c0471c21759","name":"Current date test","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":300,"wires":[]},{"id":"99f8b2657ee7f1f5","type":"http request","z":"740c0c0471c21759","name":"notification","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://api.nationalgrideso.com/dataset/e5277fca-2a2b-4836-933b-f24a676b9ed8/resource/94b4d470-a54e-41d7-ae6e-be158db9b9d4/download/dfs-industry-notification.csv","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":250,"y":240,"wires":[["0a807f10f81fdd79"]]},{"id":"0a807f10f81fdd79","type":"csv","z":"740c0c0471c21759","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":390,"y":240,"wires":[["7158b144a59e9ff4"]]},{"id":"7158b144a59e9ff4","type":"switch","z":"740c0c0471c21759","name":"Limit","property":"parts.index","propertyType":"msg","rules":[{"t":"lt","v":"20","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":240,"wires":[["03257b94a4246f1b"]]},{"id":"03257b94a4246f1b","type":"delay","z":"740c0c0471c21759","name":"","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":650,"y":240,"wires":[["270d730b187e557a"]]},{"id":"270d730b187e557a","type":"function","z":"740c0c0471c21759","name":"Combine date and time","func":"//msg.payload[\"Delivery Date\"];\n\nvar date = msg.payload[\"Requirement For\"];\n\n//var date = msg.payload[\"Requirement For\"];\nvar time = 0;\n\n//var response = (date + \" \" + time);\n\nvar response = date;\n\nmsg.payload.checked = response\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":850,"y":240,"wires":[["6623113f9da4aeeb","5b554ed4f9bf3b3f"]]},{"id":"5b554ed4f9bf3b3f","type":"debug","z":"740c0c0471c21759","name":"debug 280","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.checked","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":200,"wires":[]},{"id":"6623113f9da4aeeb","type":"moment","z":"740c0c0471c21759","name":"","topic":"","input":"payload.checked","inputType":"msg","inTz":"Europe/London","adjAmount":"0","adjType":"milliseconds","adjDir":"subtract","format":"L","locale":"en-GB","output":"ssdate","outputType":"flow","outTz":"Europe/London","x":1100,"y":240,"wires":[["ccf68115a65cc533","f290611db45bb03e"]]},{"id":"ccf68115a65cc533","type":"function","z":"740c0c0471c21759","name":"ssdate","func":"var ssdate = flow.get(\"ssdate\");\n\nmsg.payload = ssdate;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1290,"y":200,"wires":[["789998c614adef14"]]},{"id":"789998c614adef14","type":"debug","z":"740c0c0471c21759","name":"ssdate","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1410,"y":200,"wires":[]},{"id":"f290611db45bb03e","type":"function","z":"740c0c0471c21759","name":"Check Nat Grid date","func":"\nvar date1 = new Date(flow.get(\"ssdate\")).valueOf();\nvar date2 = new Date(flow.get(\"currentdate\")).valueOf();\nvar date3 = new Date(flow.get(\"ssdate\"));\n\n\nmsg.date11 = new Date(flow.get(\"ssdate\"));\nmsg.date1 = date1;\nmsg.date21 = new Date(flow.get(\"currentdate\"));\nmsg.date2 = date2;\n\nif (date1 == date2 || date1 > date2)\n{\nflow.set(\"Initial\", \"YES\");\n// @ts-ignore\nnode.status({ fill: 'green', text: date3});\n}\nelse\n{\nflow.set(\"Initial\", \"NO\");\n}\nreturn msg;\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1340,"y":240,"wires":[["b078bc3b5270cadd"]]},{"id":"b078bc3b5270cadd","type":"switch","z":"740c0c0471c21759","name":"Check Initial","property":"Initial","propertyType":"flow","rules":[{"t":"cont","v":"YES","vt":"str"},{"t":"cont","v":"NO","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":1530,"y":240,"wires":[["6d7380a83a05438b"],[]]},{"id":"dc3a83c89cb11b29","type":"debug","z":"740c0c0471c21759","name":"RESULT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1800,"y":240,"wires":[]},{"id":"6d7380a83a05438b","type":"function","z":"740c0c0471c21759","name":"Initial","func":"var ssdate = flow.get(\"Initial\");\n\nmsg.payload = ssdate;\nreturn msg;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":1670,"y":240,"wires":[["dc3a83c89cb11b29"]]}]

```

---

<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:** [1 March 2024 15:25 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/2 "2024-03-01T15:25:58Z")

</div>

Do yourself a favour and do not use locale-specific formats. Best to use ISO8601 for any date/time being exchanged. e.g. "2024-03-01T15:24:03.307Z" or just "2024-03-01" for dates. These are unambiguous and sort correctly as text. They also convert easily to Date objects.

---

<div class="post-metadata">

**Author:** ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)\
**Post date:** [1 March 2024 15:28 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/3 "2024-03-01T15:28:44Z")

</div>

How do I do that when I'm getting the data from an external and not in my control database?

---

<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:** [1 March 2024 15:29 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/4 "2024-03-01T15:29:31Z")

</div>

Perhaps you can given an example of the data you are getting? And what db type are you getting it from?

---

<div class="post-metadata">

**Author:** ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)\
**Post date:** [1 March 2024 15:30 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/5 "2024-03-01T15:30:10Z")

</div>

It's in the flow I'd attached.

[https://api.nationalgrideso.com/dataset/e5277fca-2a2b-4836-933b-f24a676b9ed8/resource/94b4d470-a54e-41d7-ae6e-be158db9b9d4/download/dfs-industry-notification.csv](https://api.nationalgrideso.com/dataset/e5277fca-2a2b-4836-933b-f24a676b9ed8/resource/94b4d470-a54e-41d7-ae6e-be158db9b9d4/download/dfs-industry-notification.csv)

---

<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:** [1 March 2024 15:53 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/6 "2024-03-01T15:53:24Z")

</div>

You can use monent in a switch node to comapre time objects, You can tell moment the format of the input date.

```auto
$moment($$.payload."Notification Issued Date" & " " & $$.payload."Notification Issued Time", "DD/MM/YYYY HH:mm").isAfter($moment())

```

`$$.payload."Notification Issued Date" & " " & $$.payload."Notification Issued Time" `  
is the same as `"01/03/2024" + " " + "12:00"`

`"DD/MM/YYYY HH:mm"` tells moment the forrmat to expect.

`$moment()` is the time/date now

example flow

```auto
[{"id":"1eedac72d8918492","type":"inject","z":"740c0c0471c21759","name":"Test","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":130,"y":200,"wires":[["0ab30c38a0de477b"]]},{"id":"0ab30c38a0de477b","type":"template","z":"740c0c0471c21759","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Notification Issued Date,Notification Issued Time,Requirement For,Requirement Type,Status,Notification Type\n02/03/2024,08:00,08/02/2024,Test,The ESO may publish a DFS Service Requirement for 08/02/2024. This will be System Tagged. ,Anticipated Requirement Notice\n01/03/2024,12:00,01/03/2024,Test,DFS Service Requirement has been published for 01/03/2024. This is System Tagged.,Requirement Published","output":"str","x":260,"y":200,"wires":[["0a807f10f81fdd79"]]},{"id":"0a807f10f81fdd79","type":"csv","z":"740c0c0471c21759","name":"","sep":",","hdrin":true,"hdrout":"none","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":410,"y":200,"wires":[["b078bc3b5270cadd"]]},{"id":"b078bc3b5270cadd","type":"switch","z":"740c0c0471c21759","name":"compare date times","property":"$moment($$.payload.\"Notification Issued Date\" & \" \" & $$.payload.\"Notification Issued Time\", \"DD/MM/YYYY HH:mm\")\t.isAfter($moment())","propertyType":"jsonata","rules":[{"t":"true"},{"t":"false"}],"checkall":"true","repair":false,"outputs":2,"x":640,"y":200,"wires":[["dc3a83c89cb11b29"],[]]},{"id":"dc3a83c89cb11b29","type":"debug","z":"740c0c0471c21759","name":"RESULT","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":600,"y":260,"wires":[]}]

```

moment docs [Is After - momentjs.com](https://momentjscom.readthedocs.io/en/latest/moment/05-query/03-is-after/)

p.s. best to create an inject with example data, as this helps others who may not want to connect to exterior site, to help you. See how i added a templatre holding example data.

---

<div class="post-metadata">

**Author:** ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)\
**Post date:** [1 March 2024 16:07 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/7 "2024-03-01T16:07:44Z")

</div>

Thank you. i'll give that a try.

Re. external database, I did think of that but wondered if it was somehow connected to how I got the data from it.

---

<div class="post-metadata">

**Author:** ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)\
**Post date:** [1 March 2024 17:57 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/8 "2024-03-01T17:57:47Z")

</div>

Thank you, that worked with a slight change to;

```auto
$moment($$.payload."Requirement For" , "DD/MM/YYYY")
.isBefore($moment().format('L'))

```

---

<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:** [1 March 2024 18:25 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/9 "2024-03-01T18:25:00Z")

</div>

I think this is more correct, as format turns the date object into a string.

```auto
$moment($$.payload."Requirement For" , "DD/MM/YYYY") .isBefore($moment().startOf("day"))

```

So is before date at 00:00 am

---

<div class="post-metadata">

**Author:** ![Tinbum1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tinbum1/32/60100_2.png) [@Tinbum1](https://discourse.nodered.org/u/Tinbum1)\
**Post date:** [1 March 2024 18:30 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/10 "2024-03-01T18:30:14Z")

</div>

Thank you again, I've learnt a great deal from you. 🙂

---

<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:** [1 March 2024 18:34 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/11 "2024-03-01T18:34:34Z")

</div>

Not a problem, its great that you experimented and came up with a solution. It's nice to help and point others in right direction.

---

<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:** [15 March 2024 18:35 UTC](https://discourse.nodered.org/t/problem-comparing-dates/86054/12 "2024-03-15T18:35:19Z")

</div>

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