Avatar of Rama Tito
Rama TitoFlag 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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

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
Avatar of Rama Tito
Rama Tito
Flag of Malaysia image

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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rama Tito
Rama Tito
Flag of Malaysia image

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

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

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

Avatar of Rama Tito
Rama Tito
Flag of Malaysia image

ASKER

I did stop and restart .

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

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

Open in new window

Avatar of Rama Tito
Rama Tito
Flag of Malaysia image

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

Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

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.
Avatar of Rama Tito
Rama Tito
Flag of Malaysia image

ASKER

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

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo