Change a string type to Date type

Hello all,

I am trying to calculate a time difference between 2 dates (that I extract from a csv file), so I need my dates to have the correct date format.

Here's their current format : dd/MM/yyyy HH:mm

image

So far I couldn't find the solution.
This is my first forum post ever and I'm a node-red beginner, so I hope I'm doing it right ^^'

Thanks a lot in advance !

I hope this helps:

[{"id":"ed19354c.502338","type":"function","z":"d1fe89f0.772168","name":"Today!","func":"var Time = new Date();\n        var Hour = Time.getHours();\n        var minute = Time.getMinutes();\n        var Sec = Time.getSeconds();\n        var Day = Time.getUTCDate();\n        var month = Time.getMonth()+1;\n        var year = Time.getFullYear();\n        if(minute<10)\n            minute = \"0\" + minute;\n        if(Sec<10)\n            Sec = \"0\" + Sec;\n        //escribiendo sobre el campo de texto la Hour actual \n      \n       var Today =(Day+\":\"+month+\":\"+year+\"\\n\"+Hour+\":\"+minute+\":\"+Sec);\n       msg.payload=Today;\nreturn msg;","outputs":1,"noerr":0,"x":480,"y":380,"wires":[["e95d533f.dd369"]]},{"id":"e95d533f.dd369","type":"debug","z":"d1fe89f0.772168","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":650,"y":380,"wires":[]},{"id":"2fd86060.c9b9a","type":"inject","z":"d1fe89f0.772168","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":320,"y":380,"wires":[["ed19354c.502338"]]}]

Thanks for your example, it helped me understand a bit more how the Date format works. Unfortunately I'm not dealing with the current time but with past times, so it doesn't seem like I can use the Date() function...

You can feed an old date into the javascript date function, a quick google will show you how

You could also use node-red-contrib-moment. Here is an example with two dates being inject in the same msg and two copies of the moment node changing the format:


and here is the flow:

[{"id":"dc5af5ba.bb9b6","type":"inject","z":"89bb7ba2.1f442","name":"","topic":"","payload":"{\"date1\":\"01/01/2018 05:05\",\"date2\":\"04/01/2018 15:56\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":60,"wires":[["1683539b.69c5ac","aad6e4b1.1bc4a8"]]},{"id":"1683539b.69c5ac","type":"debug","z":"89bb7ba2.1f442","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":290,"y":120,"wires":[]},{"id":"aad6e4b1.1bc4a8","type":"moment","z":"89bb7ba2.1f442","name":"","topic":"","input":"payload.date1","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"en_US","output":"payload.date1n","outputType":"msg","outTz":"America/New_York","x":320,"y":60,"wires":[["6ebdd87f.2614c8"]]},{"id":"6ebdd87f.2614c8","type":"moment","z":"89bb7ba2.1f442","name":"","topic":"","input":"payload.date2","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"","locale":"en_US","output":"payload.date2n","outputType":"msg","outTz":"America/New_York","x":560,"y":60,"wires":[["a6e3c74a.183b1"]]},{"id":"a6e3c74a.183b1","type":"debug","z":"89bb7ba2.1f442","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":60,"wires":[]}]
1 Like

I had tried some of the solutions I found but it didn't work for me and I couldn't find out why

This is working perfectly !! Thanks a lot :grin:

Hope this will help other people in the future !

Have a good day everyone

1 Like

Then read the section here about logging events
https://nodered.org/docs/writing-functions#adding-status

You can then add “debug lines” within your function
to understand the different steps and understand more why it isn’t working

1 Like

Thank you, I will take a look at that too when I have another issue ^^