I have a sensor that pools the payment due date in the following format 12/10/20 (DD/MM/YY- 12th October 2020) from my government's Electricity board server.
I want to create an output in Node-red which will find/calculate the Number days Remaining to pay the dues. (i.e 12th October 2020 - today's date/23 Sep 2020)
Hello,
If you are comfortable with some basic javascript you could use the build in date parsing function of javascript in a function node:
This gives you the date input parsed to standard milliseconds since midnight of the 1st of January 1970.
If you do this for both dates and save the results in two variables you can than by simple subtraction calculate the difference in milliseconds and divide the result by the number of milliseconds in a day ( 86400000) to get the number of days between the two days.
Thanks, I am not comfortable in java but I think I can manage COPY PASTE code & understand its flow
As per my understanding the Date.parse() needs date in YY/MMDD but my Sensor give the date in DD/MM/YY format hence am not able to prase the date or i get a wrong No of days.
Is there a trick to fix it?
Hi @ceaswaran this is JavaScript NOT java. There is a small difference in name but a huge difference in the acutal languages (i.e. searching for a java solution will almost always NOT work in node-red that uses JavaScript.
Use a function to convert the string to date...
var dateString = msg.payload; // << adjust to where ever your date string is in the msg
var dateParts = dateString.split("/"); // split on /
// month is 0-based, that's why we need dataParts[1] - 1
var d = new Date(+dateParts[2], dateParts[1] - 1, +dateParts[0]);
var now = new Date(); //get now date
var diff = now - d; //difference in ms
var days = diff / 86400000;
msg.payload = days; //store the days value in the msg, ready to send to next node
return msg; // send the msg to the next node