-4

I want to find the difference between two dates, inclusive. For example,

11/1/2014 (Nov 1) - 10/1/2014 (Oct 1) == 32 days

I was checking my results with the following page

var date1 = new Date(2013, 10, 01);
var date2 = new Date(2015, 10, 15);
var diff = new Date(date2.getTime() - date1.getTime());
console.log("diff.getUTCDate: " + diff.getUTCDate() );
// diff is: Thu Jul 05 1973 04:00:00 GMT+0300 (EEST)
if(diff.getUTCDate() == 1){
  console.log(diff.getUTCDate() ); // Gives day count of difference
}else{
   console.log( diff.getUTCDate() + 1 ); 
}
royhowie
  • 11,075
  • 14
  • 50
  • 67
  • 4
    Welcome. What is the question? What do you expected and what you got? He, who? – v.k. Nov 28 '14 at 01:26
  • 1
    Use [Moment.js](http://momentjs.com/) if you want sane and easy date handling. – Etheryte Nov 28 '14 at 02:05
  • @Nit the OP originally tagged it with moment.js, but I removed the tag since this question did not have anything to do with it. – royhowie Nov 28 '14 at 02:05
  • possible duplicate of [Get difference between 2 dates in javascript?](http://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – bud-e Nov 28 '14 at 02:08

3 Answers3

0

So I'm going to assume that you mean you want to calculate the number of days between 2 dates (including the end date). And you mistyped your example: between 10/01/2014 and 11/01/2014 - should return 32 days

So if that's the case, remove the time portion of the date and then do the date subtraction:

var d1 = new Date('October 01 2014'.replace('th',''));
var d2 = new Date('November 01 2014'.replace('th',''));

// remove the time portion, set the dates to midnight
d1.setHours(0,0,0,0);
d2.setHours(0,0,0,0);

var diff = Math.ceil((d2 - d1) / 86400000) + 1;

console.log(diff); //returns 32

Fiddle: http://jsfiddle.net/WJ5Rd/5/

trekforever
  • 3,914
  • 25
  • 29
0

If you don't want to do division with possibly large numbers, you can reduce the problem by making Date do some of the work for you

function isLeapYear(year) {
    if (year % 4) return false;
    if (year % 100) return true;
    if (year % 400) return false;
    return true;
}

function dayOfYear(date) {
    var d1 = Date.UTC(date.getUTCFullYear(), 0, 0),
        d2 = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
    return (d2 - d1) / 864e5;
}

function daysBetween(start, end) {
    var days = dayOfYear(end) - dayOfYear(start);
    start = start.getUTCFullYear();
    end = end.getUTCFullYear();
    for (; start < end; ++start)
        days += isLeapYear(start) ? 366 : 365;
    return days;
}

Now it is just a case of

daysBetween(new Date(2014, 10-1, 1), new Date(2014, 11-1, 1)); // 32

This is good for if your time range will span decades or centuries


Otherwise just making a quick modification to dayOfYear gives

function countDays(start, end) {
    var d1 = Date.UTC(start.getUTCFullYear(), start.getUTCMonth(), start.getUTCDate()),
        d2 = Date.UTC(end.getUTCFullYear(), end.getUTCMonth(), end.getUTCDate());
    return (d2 - d1) / 864e5;
}

Then

countDays(new Date(2014, 10-1, 1), new Date(2014, 11-1, 1)); // 32

Please note I used UTC for these calculations, this is just to avoid any daylight savings or other timezone changes

Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

Include Moment.js for sane date handling. Then it's as easy as

var a = moment([2014,12,9])
var b = moment([2014,11,3])
a.diff(b, 'days') //37
Etheryte
  • 24,589
  • 11
  • 71
  • 116