2

I want to get the difference between 2 dates in days months and years. The problem is that because there is not a set amount of days to any Month, I can't figure out how I can compare 2 dates with Months that are between 28 and 31 days. I have done it for Years and Months, because I know there are 12 Months to a year, so I can calculate it. Maybe someone knows a method of getting round this. here's what I have so far. Thanks for reading.

var date1 = new Date(2013,10,1);
var date2 = new Date();

var y1 = date1.getFullYear();
var m1 = date1.getMonth();

var y2 = date2.getFullYear();
var m2 = date2.getMonth();

var totDiff = (y1 - y2) * 12 + (m1 - m2 -1);

var yDiff = Math.floor(Math.abs(totDiff) / 12);
var mDiff = totDiff - (Math.floor((totDiff) / 12) * 12);

console.log("years: " + yDiff + " months: " + mDiff);  
Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29
user2014429
  • 2,497
  • 10
  • 35
  • 49
  • http://stackoverflow.com/questions/542938/how-do-i-get-the-number-of-days-between-two-dates-in-javascript – Shahe Dec 27 '13 at 13:18

2 Answers2

1

I would iterate a date in a 'while' loop, increasing the lower date and counting iterations needed to achieve the greater date. That way you can count the difference in any time units.

The method may look like following. Quite a lot of code, but i hope it's clear (see 'dateDiff' method below):

function addTime(date, unit){
    var date = new Date(date);
    switch (unit){
        case "day":
            date.setDate(date.getDate() + 1);
            break;
        case "month":
            date.setMonth(date.getMonth() + 1);
        break;
        case "year":
            date.setYear(date.getFullYear() + 1);
        break;
    }
    return date;
}
function dateDiff(from, to){
    var from = new Date(from),
        to = new Date(to),
        res = {
            days:0,
            months:0,
            years:0
        };

    while(addTime(from, 'year').valueOf() <= to.valueOf()){
        res.years++;
        from = addTime(from, 'year');
    }
    while(addTime(from, 'month').valueOf() <= to.valueOf()){
        res.months++;
        from = addTime(from, 'month');
    }
    while(addTime(from, 'day').valueOf() <= to.valueOf()){
        res.days++;
        from = addTime(from, 'day');
    }
    return res;
}

and usage:

dateDiff(new Date(..some date..), new Date(..some greater date..))

Example

console.log( dateDiff(new Date(2013, 11, 1), new Date()) );
//Object {days: 26, months: 0, years: 0} 

console.log( dateDiff(new Date(2000, 0, 1), new Date()) );
//Object {days: 26, months: 11, years: 13} 
Paul
  • 1,656
  • 11
  • 16
0
var today=new Date('January 01, 2014 00:00:00');
var tt=new Date();

var ss=(today-tt)/1000;
days = parseInt(ss / 86400);
   ss = ss % 86400;     
    hours = parseInt(ss / 3600);
    ss = ss % 3600;     
    minutes = parseInt(ss / 60);
    seconds = parseInt(ss % 60);
document.getElementById('d').innerHTML=days; 
document.getElementById('h').innerHTML=hours;
document.getElementById('m').innerHTML=minutes;
document.getElementById('s').innerHTML=seconds;

You can find the difference betwwen two dates in year,days,hours.mins even in secs also , by using some calculation as above.

Vivek S
  • 2,010
  • 1
  • 13
  • 15