Problem comparing dates

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.

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

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.

1 Like

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

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

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

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

$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

[{"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

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.

1 Like

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.

Thank you, that worked with a slight change to;

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

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

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

So is before date at 00:00 am

1 Like

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

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.

1 Like

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