Avatar of Rama Tito
Rama Tito
Flag for Malaysia asked on

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

Node.js* node-redJavaScript

Avatar of undefined
Last Comment
Rama Tito

8/22/2022 - Mon
Chinmay Patel

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 Tito

ASKER
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
ASKER CERTIFIED SOLUTION
Chinmay Patel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Rama Tito

ASKER
"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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Chinmay Patel

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 Tito

ASKER
I did stop and restart .

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

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

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Rama Tito

ASKER
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 Patel

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 Tito

ASKER
Good day Chinmay. Without certain points from your side I am unable to find the solution. thank you.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck