I have 2 timestamps and i already calculated the time difference in minutes with moment plugin. Now i want to convert the minutes to HH:mm.
var x = moment('10:00', 'HH:mm'); var y = moment('11:30', 'HH:mm'); var diff = y.diff(x, 'minutes'); // 90 var convert = moment.duration(diff, "minutes").format('HH:mm'); alert(convert); // should give me 01:30 but does not work
What i am doing wrong?
Advertisement
Answer
Since you have not specified what is the error, I’m assuming you are leaving the dependencies required for duration method.
The moment-duration-format depends on moment, so you should require it before using it.
npm install moment moment-duration-format
Then either you can import the dependencies or require them.
import moment from "moment"; import "moment-duration-format";
var moment = require("moment"); require("moment-duration-format"); var x = moment("10:00", "HH:mm"); var y = moment("11:30", "HH:mm"); var diff = y.diff(x, "minutes"); // 90 var convert = moment.duration(diff, "minutes").format("HH:mm"); alert(convert);
Note: require is a Node.JS function and doesn’t work in client side scripting without certain requirements. More info
Hope it helps Thanks