Compare date in payload with timestamp

Hi,

So I get a date in my payload with path: data.attributes.tasks[1].due_date with values like this: “2021-10-06T00:00:00+00:00”

What id like to do is to compare that date with the current date and if the date are today or has passed I want the flow to continue. Any idea where to start?

[{"id":"1e49efa.02e021","type":"inject","z":"f656078a.752de8","name":"yesterday","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-1","payloadType":"num","x":170,"y":368,"wires":[["a6a271e7.4a7d"]]},{"id":"aaaa042c.871538","type":"debug","z":"f656078a.752de8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":774,"y":368,"wires":[]},{"id":"ba0c2422.cbcac8","type":"function","z":"f656078a.752de8","name":"today or less?","func":"const dueDate = msg.data.attributes.tasks[1].due_date;\nconst inputDate = new Date(dueDate);\nconst todaysDate = new Date();\n\nif(inputDate <= todaysDate.setHours(0,0,0,0)) {\n    node.status({fill: 'green', text: inputDate});\n    return msg;\n}\n\nnode.status({fill: 'red', text: inputDate});","outputs":1,"noerr":0,"initialize":"","finalize":"","x":560,"y":368,"wires":[["aaaa042c.871538"]]},{"id":"81ec2806.96c698","type":"inject","z":"f656078a.752de8","name":"today","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":162,"y":400,"wires":[["a6a271e7.4a7d"]]},{"id":"5c94a226.a1393c","type":"inject","z":"f656078a.752de8","name":"tomorrow","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":172,"y":432,"wires":[["a6a271e7.4a7d"]]},{"id":"a6a271e7.4a7d","type":"function","z":"f656078a.752de8","name":"create date","func":"const d = new Date();\nd.setDate(d.getDate() + msg.payload);\nconst year = d.getFullYear();\nconst month = (d.getMonth() + 1).toString().padStart(2, '0');\nconst day = d.getDate().toString().padStart(2, '0');\ndateString = `${year}-${month}-${day}T00:00:00+00:00`;\n\nnode.status({text: dateString});\nmsg.data = {\n    attributes: {\n        tasks: [ {}, {due_date: dateString }]\n    }\n};\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":342,"y":368,"wires":[["ba0c2422.cbcac8"]]}]
1 Like

Thank you, will experiment with this.

For anyone else facing a similar issue, here is an alternative more cleaner way of doing it via code (note the example is slightly different here).

let THRESHOLD = 5           // in days

let now = new Date();
let lastChange = new Date(msg.data.last_changed);
let delta = now.getTime() - lastChange.getTime();

let hours = delta / (1000 * 60 * 60);

// create a new message object

let newMsg = {
    payload: hours < 24 * THRESHOLD,
    last_changed: msg.data.last_changed,
    delta: hours,
    entity: msg.data.entity_id
};

return newMsg;