Difference in Months between two dates in JavaScript

后端 未结 26 2580
南方客
南方客 2020-11-22 17:06

How would I work out the difference for two Date() objects in JavaScript, while only return the number of months in the difference?

Any help would be great :)

相关标签:
26条回答
  • 2020-11-22 17:58

    below logic will fetch difference in months

    (endDate.getFullYear()*12+endDate.getMonth())-(startDate.getFullYear()*12+startDate.getMonth())
    
    0 讨论(0)
  • 2020-11-22 17:59

    Here you go other approach with less looping:

    calculateTotalMonthsDifference = function(firstDate, secondDate) {
            var fm = firstDate.getMonth();
            var fy = firstDate.getFullYear();
            var sm = secondDate.getMonth();
            var sy = secondDate.getFullYear();
            var months = Math.abs(((fy - sy) * 12) + fm - sm);
            var firstBefore = firstDate > secondDate;
            firstDate.setFullYear(sy);
            firstDate.setMonth(sm);
            firstBefore ? firstDate < secondDate ? months-- : "" : secondDate < firstDate ? months-- : "";
            return months;
    }
    
    0 讨论(0)
  • 2020-11-22 17:59
    function monthDiff(d1, d2) {
    var months, d1day, d2day, d1new, d2new, diffdate,d2month,d2year,d1maxday,d2maxday;
    months = (d2.getFullYear() - d1.getFullYear()) * 12;
    months -= d1.getMonth() + 1;
    months += d2.getMonth();
    months = (months <= 0 ? 0 : months);
    d1day = d1.getDate();
    d2day = d2.getDate();
    if(d1day > d2day)
    {
        d2month = d2.getMonth();
        d2year = d2.getFullYear();
        d1new = new Date(d2year, d2month-1, d1day,0,0,0,0);
        var timeDiff = Math.abs(d2.getTime() - d1new.getTime());
              diffdate = Math.abs(Math.ceil(timeDiff / (1000 * 3600 * 24))); 
        d1new = new Date(d2year, d2month, 1,0,0,0,0);
        d1new.setDate(d1new.getDate()-1);
        d1maxday = d1new.getDate();
        months += diffdate / d1maxday;
    }
    else
    {
          if(!(d1.getMonth() == d2.getMonth() && d1.getFullYear() == d2.getFullYear()))
        {
            months += 1;
        }
        diffdate = d2day - d1day + 1;
        d2month = d2.getMonth();
        d2year = d2.getFullYear();
        d2new = new Date(d2year, d2month + 1, 1, 0, 0, 0, 0);
        d2new.setDate(d2new.getDate()-1);
        d2maxday = d2new.getDate();
        months += diffdate / d2maxday;
    }
    
    return months;
    

    }

    0 讨论(0)
  • 2020-11-22 17:59

    Any value is returned along with its absolute value.

    function differenceInMonths(firstDate, secondDate) {
        if (firstDate > secondDate) [firstDate, secondDate] = [secondDate, firstDate];
        let diffMonths = (secondDate.getFullYear() - firstDate.getFullYear()) * 12;
        diffMonths -= firstDate.getMonth();
        diffMonths += secondDate.getMonth();
        return diffMonths;
    }
     
    
    0 讨论(0)
  • 2020-11-22 18:00

    Following code returns full months between two dates by taking nr of days of partial months into account as well.

    var monthDiff = function(d1, d2) {
      if( d2 < d1 ) { 
        var dTmp = d2;
        d2 = d1;
        d1 = dTmp;
      }
    
      var months = (d2.getFullYear() - d1.getFullYear()) * 12;
      months -= d1.getMonth() + 1;
      months += d2.getMonth();
    
      if( d1.getDate() <= d2.getDate() ) months += 1;
    
      return months;
    }
    
    monthDiff(new Date(2015, 01, 20), new Date(2015, 02, 20))
    > 1
    
    monthDiff(new Date(2015, 01, 20), new Date(2015, 02, 19))
    > 0
    
    monthDiff(new Date(2015, 01, 20), new Date(2015, 01, 22))
    > 0
    
    0 讨论(0)
  • 2020-11-22 18:02

    One approach would be to write a simple Java Web Service (REST/JSON) that uses JODA library

    http://joda-time.sourceforge.net/faq.html#datediff

    to calculate difference between two dates and call that service from javascript.

    This assumes your back end is in Java.

    0 讨论(0)
提交回复
热议问题