Moment.js Get Difference Between Two Dates In The Dd:hh:mm:ss Format?
I am calculating the time until 11:59PM of the current day. Here is an example.
Solution 1:
You'll want to modify your now
variable after each diff
.
var hours = mid.diff(now, 'hours'); //Get hours 'till end of day
now.hours(now.hours() + hours);
var minutes = mid.diff(now, 'minutes');
Solution 2:
Just for comparison, here's David Stampher's moment.js example from a comment on another answer giving the time until 23:59:59.999 "today" and the same functionality in plain JS:
// Moment.js versionvar now = moment();
var mid = moment();
mid = mid.endOf('day');
var diff1 = moment.utc(moment(mid, "DD/MM/YYYY HH:mm:ss")
.diff(moment(now, "DD/MM/YYYY HH:mm:ss")))
.format("HH:mm:ss");
console.log('moment.js: ' + diff1);
// POJS versionvarz = (n) => (n<10? '0' : '') + n;
var ms = newDate().setHours(23,59,59,999) - newDate();
var diff2 = z(ms/3.6e6|0) + ':' +
z(ms%3.6e6/6e4|0) + ':' +
z(ms%6e4/1e3|0);
console.log('plain js : ' + diff2);
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
Post a Comment for "Moment.js Get Difference Between Two Dates In The Dd:hh:mm:ss Format?"