[solved] Data compare eMail / date now

hi at all,
I would like to compare two times with each other. One time is from a parsing of an eMail, the other is the current time. If the difference is greater than 20 seconds, a "true" message should be output. I don't have a solution - does anyone of you have an example?

[{"id":"391c4649.2ff96a","type":"function","z":"baa30df0.9400d","name":"Date/Time mail","func":"var D1  =   {payload: msg.header.date};\nreturn [D1];","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":200,"wires":[["91fb4a14.626178"]]},{"id":"5f482705.4e5658","type":"inject","z":"baa30df0.9400d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":190,"y":260,"wires":[["c7490f42.73715"]]},{"id":"c7490f42.73715","type":"function","z":"baa30df0.9400d","name":"Date/Time now","func":"var d = new Date();\n//var n = d.getTime();\nmsg.payload = d;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":260,"wires":[["daa9ff18.5c786"]]},{"id":"91fb4a14.626178","type":"debug","z":"baa30df0.9400d","name":"Date/Time mail","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":680,"y":200,"wires":[]},{"id":"daa9ff18.5c786","type":"debug","z":"baa30df0.9400d","name":"Date/Time now","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":680,"y":260,"wires":[]}]

The problem is you are over thinking this.

As you already have a function node where you extract the email date, you simply need to do the work there.

image

var D1 = msg.header.date.valueOf();   //email epoch
var now = Date.now();                 //now epoch
var diff = Math.abs(D1 - now) / 1000; //get difference in seconds
msg.payload = (diff > 20);            //true if >20s otherwise false.
return msg;

NOTE: if msg.header.date is a string, this will not work. you would need to wrap it with new Date(msg.header.date)

1 Like

Hi Steve-Mcl,
thank you for your flow! Works great! Yes, sometimes i'm over thinking :grinning:

1 Like

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