# Moment is not defined & format to get duration of seconds

**URL:** <https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672>\
**Category:** General\
**Created:** [3 October 2018 23:58 UTC](https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672 "2018-10-03T23:58:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [3 October 2018 23:58 UTC](https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672/1 "2018-10-03T23:58:28Z")

</div>

I am trying to subtract time now() with data from mysql.  
Over here, M\_Start = "08:15:00"  
Example Current = "22:02:39"  
My intention is to **get total seconds of the different**.  
While using moment function : error message : "ReferenceError: moment is not defined (line 22, col 9)"

> var ST\_Target;  
> var Shift\_Target;
> 
> var M\_Start = flow.get('Morning\_start');  
> var M\_Stop =flow.get('Morning\_end');  
> var M\_sec = flow.get('Morning\_sec');
> 
> var M\_cycle = flow.get("Cycle\_Time");
> 
> var dt = new Date();  
> var hour = dt.getHours();  
> hour = ("0" + hour).slice(-2);
> 
> var minute = dt.getMinutes();  
> minute=("0" +minute).slice(-2);
> 
> var sec = dt.getSeconds();  
> sec=("0" + sec).slice(-2);
> 
> var current = hour+":" +minute+":"+sec;
> 
> var d = moment.duration(current.diff(M\_Start[0]));

* * *

I did install the module from the link below.

> **[node-red-contrib-moment](https://www.npmjs.com/package/node-red-contrib-moment)**
>
> Node-Red Node that produces formatted Date/Time output using the Moment.JS library. Timezone, dst and locale aware.. Latest version: 5.0.0, last published: 5 months ago. Start using node-red-contrib-moment in your project by running \`npm i...

> var momentjs = require("node-red-contrib-moment");
> 
> var ST\_Target;  
> var Shift\_Target;
> 
> var M\_Start = flow.get('Morning\_start');  
> var M\_Stop =flow.get('Morning\_end');  
> var M\_sec = flow.get('Morning\_sec');  
> .....

The error still persist -\> "ReferenceError: require is not defined (line 1, col 16)"

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [4 October 2018 00:03 UTC](https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672/2 "2018-10-04T00:03:27Z")

</div>

What versions of NR, Node and what OS are you running on ??

Craig

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 October 2018 07:25 UTC](https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672/3 "2018-10-04T07:25:17Z")

</div>

Hi @tramanah

Installing `node-red-contrib-moment` will add the Moment nodes to your palette. It does _not_ cause the `moment` library to be available in the Function node.

The Function node _cannot_ `require` modules directly. If you really want to install another npm module and make it available in the Function node, the docs explain how to do that: [https://nodered.org/docs/writing-functions#loading-additional-modules](https://nodered.org/docs/writing-functions#loading-additional-modules)

---

<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:** [4 October 2018 09:25 UTC](https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672/4 "2018-10-04T09:25:49Z")

</div>

Feed the two times into `new Date()` to create two JavaScript date/time objects. You can then subtract them and get the difference in milliseconds.

---

<div class="post-metadata">

**Author:** ![tramanah](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tramanah/32/9122_2.png) [@tramanah](https://discourse.nodered.org/u/tramanah)\
**Post date:** [5 October 2018 01:21 UTC](https://discourse.nodered.org/t/moment-is-not-defined-format-to-get-duration-of-seconds/3672/5 "2018-10-05T01:21:36Z")

</div>

thank you, its works

var current = hour+":" +minute+":"+sec;  
var curr = current.split(":");  
var Mor = M\_Start[0].split(":");

var startCurr = new Date(0,0,0, curr[0],curr[1],curr[2]);  
var startMor = new Date(0,0,0, Mor[0],Mor[1],Mor[2]);

var diffM = startCurr.getTime() - startMor.getTime();  
var Min = Math.floor(diffM/1000/60);
