12

How do I calculate difference in months in Javascript?

Please note there are similar questions such as: What's the best way to calculate date difference in Javascript

But these are based around MS difference, when some months have different number of days than others!

Any easy way to calculate month difference between 2 dates?

Just to be clear, I need to know how many months the dates span, for example:

Jan 29th 2010, and Feb 1st 2010 = 2 months
Jan 1st 2010, and Jan 2nd 2010 = 1 month
Feb 14th 2010, Feb 1st 2011 = 13 months
Feb 1st 2010, March 30th 2011 = 14 months
Community
  • 1
  • 1
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
  • What exactly do you mean by month difference? Whole calendar months eg 12Apr2010 -> 16Jun2010 = 1 ; or month lengths taken from the start date eg 12Apr2010 -> 16Jun2010 = 2. Or are you wanting to calculate decimal part also? In which case. What value does each day get? Does a month in Feb get 1/28 whereas a month in december gets 1/31? Then there's leap years... – El Ronnoco Nov 30 '10 at 10:52
  • http://stackoverflow.com/questions/2536379/difference-in-months-between-two-dates-in-javascript – ArK Nov 30 '10 at 10:52
  • Sorry, to be clear it's the total # months the date ranges span. – Tom Gullen Nov 30 '10 at 10:53

4 Answers4

22
DisplayTo.getMonth() - DisplayFrom.getMonth() + (12 * (DisplayTo.getFullYear() - DisplayFrom.getFullYear())));

getMonth minus getMonth gives you the month difference between the dates two months.

We then multiply 12 by the number of years difference and add this to the result giving us the full month span.

Tom Gullen
  • 61,249
  • 84
  • 283
  • 456
5

[edit] Based on comment, I stand corrected. Using the accepted answer I'd use somethng like:

var  datefrom = new Date('2001/03/15')
    ,dateto = new Date('2011/07/21')
    ,nocando = datefrom<dateto ? null : 'datefrom > dateto!'
    ,diffM = nocando || 
             dateto.getMonth() - datefrom.getMonth() 
              + (12 * (dateto.getFullYear() - datefrom.getFullYear()))
    ,diffY = nocando || Math.floor(diffM/12)
    ,diffD = dateto.getDate()-datefrom.getDate()
    ,diffYM = nocando || 
               (diffY>0 ? ' Year(s) ' : '')
               + diffM%12+' Month(s) '+(diffD>0? (diffD+' day(s)') : '') ;

 console.log(diffYM); //=> 10 Year(s) 4 Month(s) 6 day(s)
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • this is wrong if the 2 dates is : date1 = new Date('2011/1/04') ,date2 = new Date('2010/12/04') it will give wrong answer – ToDayIsNow Sep 11 '11 at 08:36
  • You're absolutely right, better use Gullens oneliner. Edited answer based on that. – KooiInc Sep 11 '11 at 09:30
3

I found the following on the website http://ditio.net/2010/05/02/javascript-date-difference-calculation/:

inMonths: function(d1, d2) {
        var d1Y = d1.getFullYear();
        var d2Y = d2.getFullYear();
        var d1M = d1.getMonth();
        var d2M = d2.getMonth();

        return (d2M+12*d2Y)-(d1M+12*d1Y);
    }

In your case, since you want to include all months in the date span, I would just modify the above code by adding 1 to it: return (d2M+12*d2Y)-(d1M+12*d1Y) + 1;

Marina
  • 3,222
  • 5
  • 25
  • 35
1
function calcualteMonthYr(){
    var fromDate =new Date($('#txtDurationFrom2').val()); // Date picker (text fields)
    var toDate = new Date($('#txtDurationTo2').val());
    var months=0;
        months = (toDate.getFullYear() - fromDate.getFullYear()) * 12;
        months -= fromDate.getMonth();
        months += toDate.getMonth();
            if (toDate.getDate() < fromDate.getDate()){
                months--;
            }
    $('#txtTimePeriod2').val(months); // result
}