moment is not defined & format to get duration of seconds

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

Rama TitoProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
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
0
Rama TitoProgrammerAuthor 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
0
Chinmay PatelChief Technical NinjaCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Rama TitoProgrammerAuthor 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

0
Chinmay PatelChief Technical NinjaCommented:
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

0
Rama TitoProgrammerAuthor Commented:
I did stop and restart .

The error still persist -> "ReferenceError: require is not defined (line 1, col 16)"
0
Chinmay PatelChief Technical NinjaCommented:
Please remove that line
var momentjs = require("node-red-contrib-moment");

Open in new window

0
Rama TitoProgrammerAuthor 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

0
Chinmay PatelChief Technical NinjaCommented:
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.
0
Rama TitoProgrammerAuthor Commented:
Good day Chinmay. Without certain points from your side I am unable to find the solution. thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Node.js

From novice to tech pro — start learning today.