How to subtract date/time in JavaScript?

JavascriptDate

Javascript Problem Overview


I have a field at a grid containing date/time and I need to know the difference between that and the current date/time. What could be the best way of doing so?

The dates are stored like "2011-02-07 15:13:06".

Javascript Solutions


Solution 1 - Javascript

This will give you the difference between two dates, in milliseconds

var diff = Math.abs(date1 - date2);

In your example, it'd be

var diff = Math.abs(new Date() - compareDate);

You need to make sure that compareDate is a valid Date object.

Something like this will probably work for you

var diff = Math.abs(new Date() - new Date(dateStr.replace(/-/g,'/')));

i.e. turning "2011-02-07 15:13:06" into new Date('2011/02/07 15:13:06'), which is a format the Date constructor can comprehend.

Solution 2 - Javascript

You can just substract two date objects.

var d1 = new Date(); //"now"
var d2 = new Date("2011/02/01");  // some date
var diff = Math.abs(d1-d2);  // difference in milliseconds

Solution 3 - Javascript

Unless you are subtracting dates on same browser client and don't care about edge cases like day light saving time changes, you are probably better off using moment.js which offers powerful localized APIs. For example, this is what I have in my utils.js:

subtractDates: function(date1, date2) {
    return moment.subtract(date1, date2).milliseconds();
},
millisecondsSince: function(dateSince) {
    return moment().subtract(dateSince).milliseconds();
},

Solution 4 - Javascript

If you wish to get difference in wall clock time, for local timezone and with day-light saving awareness.


Date.prototype.diffDays = function (date: Date): number {



var utcThis = Date.UTC(this.getFullYear(), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(), this.getMilliseconds());
var utcOther = Date.UTC(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds());

return (utcThis - utcOther) / 86400000;




};


Test


it('diffDays - Czech DST', function () {
// expect this to parse as local time
// with Czech calendar DST change happened 2012-03-25 02:00
var pre = new Date('2012/03/24 03:04:05');
var post = new Date('2012/03/27 03:04:05');



// regardless DST, you still wish to see 3 days
expect(pre.diffDays(post)).toEqual(-3);




});

});

Diff minutes or seconds is in same fashion.

Solution 5 - Javascript

You can use getTime() method to convert the Date to the number of milliseconds since January 1, 1970. Then you can easy do any arithmetic operations with the dates. Of course you can convert the number back to the Date with setTime(). See here an example.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAhmad FaridView Question on Stackoverflow
Solution 1 - JavascriptDavid HedlundView Answer on Stackoverflow
Solution 2 - JavascriptpawelView Answer on Stackoverflow
Solution 3 - JavascriptShital ShahView Answer on Stackoverflow
Solution 4 - JavascriptPavel SavaraView Answer on Stackoverflow
Solution 5 - JavascriptOlegView Answer on Stackoverflow