We help IT Professionals succeed at work.

moment is not defined & format to get duration of seconds

Rama Tito
Rama Tito asked
on
437 Views
Last Modified: 2018-10-04
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

Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019

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
CERTIFIED EXPERT
Distinguished Expert 2019

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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.