Hi,
I'm creating a data collection system for work that involves reading data from PLCs and PCs and put it on the dashboard, as well as into a centralised database.
We have several "smart" storage units that store material racks. These are using an SQL express database to store the information on each rack, including the type of material, and the time in which the rack was inserted in the storage unit shelf. I get this as an array of objects, which later I process in a function to get an HTML string for a table and display through a text node.
The output table looks like this:
In the object I get from the supplier, they offer me a timestamp that says when the material was placed in the storage. For some reason, the supplier used a non-standard format for the timestamps, which basically comes down to a string of numbers (no spaces, dashes, or any other characters) as in "yyyymmddhhmmss". In order to fix the timestamp and convert it into ISO format, I split the string into each part, parse it into an integer value, and create a date object from there. This is the easy part, and it works.
Now, on the dashboard I don't want to show that date, but rather the days, hours, minutes, and seconds since the rack was inserted. For this, I have to calculate the time difference between the current moment and the timestamp in the database.
The code I'm using is this:
var i;
var nowcomp = new Date().getTime();
var targetdate, tmpdate;
var timestr, elapsed = "";
var yyyy, mm, dd, hh, mn, ss;
for (i = 0; i < 20; i++) {
timestr = msg.payload[i].PutTime;
if (timestr !== null){
yyyy = timestr.substring(0,4).parseInt;
mm = timestr.substring(4,6).parseInt;
dd = timestr.substring(6,8).parseInt;
hh = timestr.substring(8,10).parseInt;
mn = timestr.substring(10,12).parseInt - 1;
ss = timestr.substring(12,14).parseInt;
tmpdate = new Date(yyyy,mm,dd,hh,mn,ss).getTime();
targetdate = Date.parse(nowcomp) - Date.parse(tmpdate);
eldays = Math.trunc(targetdate/86400000);
targetdate = targetdate - (eldays*86400000);
elhours = Math.trunc(targetdate/3600000);
targetdate = targetdate - (elhours*3600000);
elmins = Math.trunc(targetdate/60000);
targetdate = targetdate - (elmins*6000);
elsecs = Math.trunc(targetdate/1000);
elapsed = eldays + "d " + elhours + "h " + elmins + "m " + elsecs + "s";
msg.payload[i].elapsedTime = elapsed;
msg.payload[i].targetdate = Date.parse(targetdate);
} else{
msg.payload[i].elapsedTime = null;
}
}
return [msg,{payload:{now:nowcomp}}];
I have two exits, one for getting the objects and one for debugging the time format.
The time format in the second exit looks OK, giving me the time as an absolute value in milliseconds (although node-red switches between that format and others if I click on top of the value).
However, in the output of the function I get the array of objects as expected, but the difference between time values gives me the "NaN" (not a number) result, and therefore I can't calculate the values I need.
This is the output:
I've been doing searches online and trying different methods, but nothing seems to work. I saw that there's a moment node, but that currently I can't use the moment methods inside a function node.
Any idea why this is not working and how could I solve it?
Thanks for the input.