Difference between two dates in years, months, days in JavaScript

前端 未结 26 2463
执念已碎
执念已碎 2020-11-22 07:18

I\'ve been searching for 4 hours now, and have not found a solution to get the difference between two dates in years, months, and days in JavaScript, like: 10th of April 201

相关标签:
26条回答
  • 2020-11-22 08:03

    I think you are looking for the same thing that I wanted. I tried to do this using the difference in milliseconds that javascript provides, but those results do not work in the real world of dates. If you want the difference between Feb 1, 2016 and January 31, 2017 the result I would want is 1 year, 0 months, and 0 days. Exactly one year (assuming you count the last day as a full day, like in a lease for an apartment). However, the millisecond approach would give you 1 year 0 months and 1 day, since the date range includes a leap year. So here is the code I used in javascript for my adobe form (you can name the fields): (edited, there was an error that I corrected)

    var f1 = this.getField("LeaseExpiration");
    var g1 = this.getField("LeaseStart");
    
    
    var end = f1.value
    var begin = g1.value
    var e = new Date(end);
    var b = new Date(begin);
    var bMonth = b.getMonth();
    var bYear = b.getFullYear();
    var eYear = e.getFullYear();
    var eMonth = e.getMonth();
    var bDay = b.getDate();
    var eDay = e.getDate() + 1;
    
    if ((eMonth == 0)||(eMonth == 2)||(eMonth == 4)|| (eMonth == 6) || (eMonth == 7) ||(eMonth == 9)||(eMonth == 11))
    
    {
    var eDays =  31;
    }
    
    if ((eMonth == 3)||(eMonth == 5)||(eMonth == 8)|| (eMonth == 10))
    
    {
    var eDays = 30;
    }
    
    if (eMonth == 1&&((eYear % 4 == 0) && (eYear % 100 != 0)) || (eYear % 400 == 0))
    {
    var eDays = 29;
    }
    
    if (eMonth == 1&&((eYear % 4 != 0) || (eYear % 100 == 0)))
    {
    var eDays = 28;
    }
    
    
    if ((bMonth == 0)||(bMonth == 2)||(bMonth == 4)|| (bMonth == 6) || (bMonth == 7) ||(bMonth == 9)||(bMonth == 11))
    
    {
    var bDays =  31;
    }
    
    if ((bMonth == 3)||(bMonth == 5)||(bMonth == 8)|| (bMonth == 10))
    
    {
    var bDays = 30;
    }
    
    if (bMonth == 1&&((bYear % 4 == 0) && (bYear % 100 != 0)) || (bYear % 400 == 0))
    {
    var bDays = 29;
    }
    
    if (bMonth == 1&&((bYear % 4 != 0) || (bYear % 100 == 0)))
    {
    var bDays = 28;
    }
    
    
    var FirstMonthDiff = bDays - bDay + 1;
    
    
    if (eDay - bDay < 0)
    {
    
    eMonth = eMonth - 1;
    eDay = eDay + eDays;
    
    }
    
    var daysDiff = eDay - bDay;
    
    if(eMonth - bMonth < 0)
    {
    eYear = eYear - 1;
    eMonth = eMonth + 12;
    }
    
    var monthDiff = eMonth - bMonth;
    
    var yearDiff = eYear - bYear;
    
    if (daysDiff == eDays)
    {
    daysDiff = 0;
    monthDiff = monthDiff + 1;
    
    if (monthDiff == 12)
    {
    monthDiff = 0;
    yearDiff = yearDiff + 1;
    }
    
    }
    
    if ((FirstMonthDiff != bDays)&&(eDay - 1 == eDays))
    
    {
    daysDiff = FirstMonthDiff;
    
    }
    event.value = yearDiff + " Year(s)" + " " + monthDiff + " month(s) " + daysDiff + " days(s)"
    
    0 讨论(0)
  • 2020-11-22 08:03

    Yet another solution, based on some PHP code. The strtotime function, also based on PHP, can be found here: http://phpjs.org/functions/strtotime/.

    Date.dateDiff = function(d1, d2) {
        d1 /= 1000;
        d2 /= 1000;
        if (d1 > d2) d2 = [d1, d1 = d2][0];
    
        var diffs = {
            year: 0,
            month: 0,
            day: 0,
            hour: 0,
            minute: 0,
            second: 0
        }
    
        $.each(diffs, function(interval) {
            while (d2 >= (d3 = Date.strtotime('+1 '+interval, d1))) {
                d1 = d3;
                ++diffs[interval];
            }
        });
    
        return diffs;
    };
    

    Usage:

    > d1 = new Date(2000, 0, 1)
    Sat Jan 01 2000 00:00:00 GMT+0100 (CET)
    
    > d2 = new Date(2013, 9, 6)
    Sun Oct 06 2013 00:00:00 GMT+0200 (CEST)
    
    > Date.dateDiff(d1, d2)
    Object {
      day: 5
      hour: 0
      minute: 0
      month: 9
      second: 0
      year: 13
    }
    
    0 讨论(0)
提交回复
热议问题