# Timestamp in function problems

**URL:** <https://discourse.nodered.org/t/timestamp-in-function-problems/34638>\
**Category:** General\
**Created:** [21 October 2020 14:19 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638 "2020-10-21T14:19:08Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [21 October 2020 14:19 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/1 "2020-10-21T14:19:08Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff83d67b5abf1d996faf90b1c5ba7e3e13ea2f85.png)

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:

```auto
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:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/550ca8b8e9bd65036890e391d204e9564ecbb7b4.png)

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.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 October 2020 14:38 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/2 "2020-10-21T14:38:41Z")

</div>

Try putting some `node.warn(....)` statements in the loop to see what your intermediate vars are giving you. At first sight, it looks like you are trying to create variables within the loop without using `var` or `let` or `const` which may have an impact.

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [21 October 2020 14:43 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/3 "2020-10-21T14:43:37Z")

</div>

I am using "new" in the following statement

```auto
tmpdate = new Date(yyyy,mm,dd,hh,mn,ss).getTime();

```

only because I read in one of the forums I checked that unless I did that, I would not get the integer value, but a whole date object.

Besides that, I'm not declaring any variables or constants inside the loop, they're all declared in the beginning.

I have no idea what's wrong in there.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 October 2020 14:47 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/4 "2020-10-21T14:47:51Z")

</div>

> [@OriolFM](#):
>
> `eldays`

Appears, amongst others not to be declared unless there is more code you haven't shared? Best to use `let` or `const` inside the loop so that you get a clean var each time through the loop (unless your loop is slow).

Put in the warn statements so that you can see what is happening inside the loop.

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [21 October 2020 15:13 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/5 "2020-10-21T15:13:35Z")

</div>

Okay, so I updated the code to this:

```auto
var i;
var nowcomp = new Date().getTime();
node.warn("nowcomp: " + nowcomp);
node.warn("nowcomp parsed: " + Date.parse(nowcomp));
var targetdate, tmpdate;
var timestr, elapsed = "";

for (i = 0; i < 20; i++) {
    let yyyy, mm, dd, hh, mn, ss, eldays, elmins, elsecs;
    timestr = msg.payload[i].PutTime;
    if (timestr !== null){
        node.warn("timestr:" + timestr);
        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();
        node.warn("tmpdate:" + tmpdate);
        node.warn("tmpdate parsed: " + Date.parse(tmpdate));
        targetdate = Date.parse(nowcomp) - Date.parse(tmpdate);
        node.warn("targetdate: " + targetdate);
        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}}];

```

And the output I get for this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/9/a92171d214d28f164b1885d0dbba505a97b5d256.png)  
so it is not recognizing the result of the.getTime() method as a number (and neither does after parsing it).

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 October 2020 15:22 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/6 "2020-10-21T15:22:34Z")

</div>

```auto
        msg.payload[i].targetdate = Date.parse(targetdate);

```

has been reduce to seconds by your elhours section.

Try this

```auto
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 = Number(timestr.substr(0,4));
        mm = Number(timestr.substr(4,2));
        dd = Number(timestr.substr(6,2));
        hh = Number(timestr.substr(8,2));
        mn = Number(timestr.substr(10,2));
        ss = Number(timestr.substr(12,2));
        tmpdate = new Date(yyyy, mm - 1, dd, hh, mn, ss).getTime();
        targetdate = nowcomp - 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*60000);
        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); // this still wont work due to reducing it to milliseconds
    } else{
        msg.payload[i].elapsedTime = null;
    }
}

return [msg,{payload:{now:nowcomp}}];

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 October 2020 15:25 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/7 "2020-10-21T15:25:05Z")

</div>

I think you will find that it has failed to parse the date.

So check to see what your y, m, d, ... values all contain, probably not what you think.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 October 2020 16:17 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/8 "2020-10-21T16:17:21Z")

</div>

Then have a look at [https://www.w3schools.com/jsref/jsref\_parseint.asp](https://www.w3schools.com/jsref/jsref_parseint.asp)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [21 October 2020 17:47 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/9 "2020-10-21T17:47:33Z")

</div>

Hi Orio,

I didnt study your javascript in depth but if it was me i would go to the original idea of using the **Moment js** library that makes datetime calculations easier with its extended library.

In order for the module library to be accesible is your function nodes you edit the **functionGlobalContext** section of the Node-red **settings.js** file :

```auto
 functionGlobalContext: {
           moment:require('moment')
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

```

**and in the Function node :**

```javascript
let moment = global.get('moment')

let currentTime = moment();
let sqlTime = moment('20201021075538','YYYYMMDDHHmmss'); // convert to moment
let duration = sqlTime.from(currentTime)

node.warn(currentTime)
node.warn(sqlTime)
node.warn(`Duration : ${duration}`)

//node.send({'payload': `This is the year: ${moment().format('YYYY')} as a string`})

```

**Example flow :**

```auto
[{"id":"bb4051ca.65a4e8","type":"inject","z":"54aa25ce.417e1c","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":300,"y":380,"wires":[["345d113b.aaa426"]]},{"id":"345d113b.aaa426","type":"function","z":"54aa25ce.417e1c","name":"","func":"let moment = global.get('moment')\n\nlet currentTime = moment();\nlet sqlTime = moment('20101021075538','YYYYMMDDHHmmss');\nlet duration = sqlTime.from(currentTime)\n\nnode.warn(currentTime)\nnode.warn(sqlTime)\nnode.warn(`Duration : ${duration}`)\n\n//node.send({'payload': `This is the year: ${moment().format('YYYY')} as a string`})","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":380,"wires":[["9cba50f8.4d01d8"]]},{"id":"9cba50f8.4d01d8","type":"debug","z":"54aa25ce.417e1c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":380,"wires":[]}]

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/d/5d75f513489c384c4053ea8aaf39e2b46eb4d689.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 October 2020 20:27 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/10 "2020-10-21T20:27:10Z")

</div>

As informed by @Paul-Reed the change node has $moments() in the JSONata expression.  
e.g.

```auto
[{"id":"c7fe2615.e38278","type":"inject","z":"8d22ae29.7df6d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20201015120000","payloadType":"str","x":220,"y":1960,"wires":[["e23e595e.a3d348"]]},{"id":"e23e595e.a3d348","type":"change","z":"8d22ae29.7df6d","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t $currentTime := $moment().tz('Europe/London').format();\t $duration := $moment(payload,'YYYYMMDDHHmmss').fromNow($currentTime);\t {\t \"current\": $currentTime,\t \"duration\": $duration\t }\t)\t\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":1960,"wires":[["49952583.65b3fc"]]},{"id":"49952583.65b3fc","type":"debug","z":"8d22ae29.7df6d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":1960,"wires":[]}]

```

> [@Formatting times & dates with moment.js examples](https://discourse.nodered.org/t/formatting-times-dates-with-moment-js-examples/29553):
>
> Since node-RED v1.1.0 support for the moment.js date/time library has been added, currently only available for jsonata expressions, but hopefully in time to also be added to the function node. I've included a few examples below, of how Moment can be used to provide date/time functions. Please feel free to add others This is the basic format, which creates a realtime UTC timestamp similar to Sun Jul 05 2020 13:22:30 GMT+0000. [moment] [{"id":"54c46ebe.3b623","type":"change","z":"a444a9ff.e7a…

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [22 October 2020 06:20 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/11 "2020-10-22T06:20:54Z")

</div>

It worked like a charm, thank you very much!

---

<div class="post-metadata">

**Author:** ![OriolFM](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/oriolfm/32/30710_2.png) [@OriolFM](https://discourse.nodered.org/u/OriolFM)\
**Post date:** [22 October 2020 06:25 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/12 "2020-10-22T06:25:21Z")

</div>

Thank you, it is good to know that I can call functions from that library. The issue is that node-red is running on a company server over which I do not have admin rights, so I am limited to what I can do from the web interface (install new nodes by the "manage pallette" menu is not a problem, but directly accessing the server is out of the question for me, and would like to avoid bothering the sysadmin for issues I can solve in another way).

In any case, should I need other, more advanced stuff regarding times and dates, I'll keep that in mind.

Thank you all for your replies! 😃

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 November 2020 06:25 UTC](https://discourse.nodered.org/t/timestamp-in-function-problems/34638/13 "2020-11-05T06:25:23Z")

</div>

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