moment is not defined & format to get duration of seconds

Rama Tito
Rama Tito used Ask the Experts™
on
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]));

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Rama,

Have you added the reference to Moment.js library in your code? If not, please add that reference and this error will go away.

Regards,
Chinmay
Rama TitoProgrammer

Author

Commented:
Hi Chinmay,

I am doing in node-red and script in function palette. I did install moment palette. May i know to add in moment.js
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Rama,

How did you install the moment palette? may I know the command you used?

I am hoping you have used: https://www.npmjs.com/package/node-red-contrib-moment

After that you need to hint Node that we need to use this package
var momentjs = require("node-red-contrib-moment")

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Rama TitoProgrammer

Author

Commented:
"ReferenceError: require is not defined (line 1, col 16)"

I did install the packages and insert line

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');

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]));

msg.payload =d;

Open in new window

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Did you stop and restart Node-RED after adding the library?

Also, comment out
var momentjs = require("node-red-contrib-moment");

Open in new window

Rama TitoProgrammer

Author

Commented:
I did stop and restart .

The error still persist -> "ReferenceError: require is not defined (line 1, col 16)"
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Please remove that line
var momentjs = require("node-red-contrib-moment");

Open in new window

Rama TitoProgrammer

Author

Commented:
hi , I did work around with this method and 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);

Open in new window

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
In that case, please accept your comment as an answer. Thanks for accepting my comment as an answer but if it did not help you, then you should not allocate it points.
Rama TitoProgrammer

Author

Commented:
Good day Chinmay. Without certain points from your side I am unable to find the solution. thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial