# Get difference between 2 dates in JavaScript?

## The Question :

608 people think this question is useful

How do I get the difference between 2 dates in full days (I don’t want any fractions of a day)

var date1 = new Date('7/11/2010');
var date2 = new Date('12/12/2010');
var diffDays = date2.getDate() - date1.getDate();



I tried the above but this did not work.

• Just a side note: do not create Date objects with these kind of strings as input; it’s non-standard and up to the browser how those are parsed. Use strings that can be parsed by Date.parse or, rather, use three numeric values as the arguments to new Date, e.g. new Date(2010, 11, 7);.
• Side-note: Never trust the system time on client-side! It’s wrong quite often, you might break your application.
• Also, try a small function at stackoverflow.com/a/53092438/3787376.
• @Edward, you really call that a SMALL function? 🙂

929 people think this answer is useful

const date1 = new Date('7/13/2010');
const date2 = new Date('12/15/2010');
const diffTime = Math.abs(date2 - date1);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffTime + " milliseconds");
console.log(diffDays + " days");


Observe that we need to enclose the date in quotes. The rest of the code gets the time difference in milliseconds and then divides to get the number of days. Date expects mm/dd/yyyy format.

788 people think this answer is useful

# A more correct solution

since dates naturally have time-zone information, which can span regions with different day light savings adjustments

Previous answers to this question don’t account for cases where the two dates in question span a daylight saving time (DST) change. The date on which the DST change happens will have a duration in milliseconds which is != 1000*60*60*24, so the typical calculation will fail.

You can work around this by first normalizing the two dates to UTC, and then calculating the difference between those two UTC dates.

Now, the solution can be written as,

const _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
// Discard the time and time-zone information.
const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

return Math.floor((utc2 - utc1) / _MS_PER_DAY);
}

// test it
const a = new Date("2017-01-01"),
b = new Date("2017-07-25"),
difference = dateDiffInDays(a, b);



This works because UTC time never observes DST. See Does UTC observe daylight saving time?

p.s. After discussing some of the comments on this answer, once you’ve understood the issues with javascript dates that span a DST boundary, there is likely more than just one way to solve it. What I provided above is a simple (and tested) solution. I’d be interested to know if there is a simple arithmetic/math based solution instead of having to instantiate the two new Date objects. That could potentially be faster.

116 people think this answer is useful

Here is a solution using moment.js:

var a = moment('7/11/2010','M/D/YYYY');
var b = moment('12/12/2010','M/D/YYYY');
var diffDays = b.diff(a, 'days');



I used your original input values, but you didn’t specify the format so I assumed the first value was July 11th. If it was intended to be November 7th, then adjust the format to D/M/YYYY instead.

38 people think this answer is useful
var date1 = new Date("7/11/2010");
var date2 = new Date("8/11/2010");
var diffDays = parseInt((date2 - date1) / (1000 * 60 * 60 * 24), 10);



13 people think this answer is useful

I tried lots of ways, and found that using datepicker was the best, but the date format causes problems with JavaScript….

So here’s my answer and can be run out of the box.

<input type="text" id="startdate">
<input type="text" id="enddate">
<input type="text" id="days">

<script src="https://code.jquery.com/jquery-1.8.3.js"></script>
<script src="https://code.jquery.com/ui/1.10.0/jquery-ui.js"></script>
<script>
$(document).ready(function() {$( "#startdate,#enddate" ).datepicker({
changeMonth: true,
changeYear: true,
firstDay: 1,
dateFormat: 'dd/mm/yy',
})

$( "#startdate" ).datepicker({ dateFormat: 'dd-mm-yy' });$( "#enddate" ).datepicker({ dateFormat: 'dd-mm-yy' });

$('#enddate').change(function() { var start =$('#startdate').datepicker('getDate');
var end   = $('#enddate').datepicker('getDate'); if (start<end) { var days = (end - start)/1000/60/60/24;$('#days').val(days);
}
else {
alert ("You cant come back before you have been!");
$('#startdate').val("");$('#enddate').val("");
\$('#days').val("");
}
}); //end change function
</script>



a Fiddle can be seen here DEMO

3 people think this answer is useful

This is the code to subtract one date from another. This example converts the dates to objects as the getTime() function won’t work unless it’s an Date object.

    var dat1 = document.getElementById('inputDate').value;
var date1 = new Date(dat1)//converts string to date object