Reputation: 13
I need to find days difference between two dates using javascript, here is my code
I have start date and end date
var diff = Math.floor((Date.parse(enddate) - Date.parse(startdate)) / 86400000);
Its calculating the difference from current time. I need to find the number of dates between given dates.
For example if i give input start date as 17-dec-2014 and 19-dec-2014 its displaying two days, but i need to calculate number of days 17,18 and 19. It should display number of days as three.
Any one help me please?
Upvotes: 1
Views: 577
Reputation: 1074295
You can set the hours, minutes, seconds, and milliseconds to 0 before doing the comparison in order to ignore the time of day, e.g.:
var startdate = "2014-12-17";
var enddate = "2014-12-19";
var start = new Date(startdate);
start.setHours(0, 0, 0, 0); // Sets hours, minutes, seconds, and milliseconds
var end = new Date(enddate);
end.setHours(0, 0, 0, 0);
var diff = Math.round((end - start) / 86400000) + 1; // See note below re `+ 1`
snippet.log("diff = " + diff); // 3
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
Two notes on that:
Math.round
: This is here because if the timespan crosses a Daylight Savings Time boundary, the number will be off by a small fraction, but within the realm where rounding corrects it. Note that you must round, not truncate, floor, ceiling.
+ 1
: The + 1
at the end of the diff =
line is because your "difference" is unusual, because you're counting the start and end days inclusive. That's very odd, it would say that the difference in days from one Monday to the next was eight, not seven, because it would count the Monday on either end. But you said:
For example if i give input start date as 17-dec-2014 and 19-dec-2014 its displaying two days, but i need to calculate number of days 17,18 and 19.
...so you need the + 1
. A normal difference between two dates wouldn't have it.
Example across DST boundaries (in many timezones):
var start, end, diff;
start = new Date(2014, 2, 1); // March 1st 2014
end = new Date(2014, 5, 1); // May 1st 2014
diff = ((end - start) / (1000 * 3600 * 24)) + 1;
// diff won't *quite* be 93, because of the change to DST
// (assuming a timezone where DST changes sometime in
// March, as in most parts of the U.S., UK, and Canada
snippet.log("diff = " + diff + " instead of 93");
snippet.log("rounded = " + Math.round(diff));
// Similarly, at the other end:
start = new Date(2014, 9, 1); // October 1st 2014
end = new Date(2014, 11, 1); // December 1st 2014
diff = ((end - start) / (1000 * 3600 * 24)) + 1;
// diff won't *quite* be 62, because of the change to DST
// (assuming a timezone where DST changes sometime in
// March, as in most parts of the U.S., UK, and Canada
snippet.log("diff = " + diff + " instead of 62");
snippet.log("rounded = " + Math.round(diff));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
This is the kind of thing that makes me turn to a library like MomentJS
. Using MomentJS, it would be:
var diff = moment(enddate).diff(moment(startdate), 'days') + 1;
...where again the + 1
is because of your unusual definition of the difference between two dates.
Upvotes: 5