How does js calculate the years, months and days between two dates (including the Pingrun year 28 29 30 31)

the ones on the Internet are so rude that they don"t consider asking for advice from the boss of the year and month

Jun.02,2021

this thing depends on what you think.

you want one year, two months and three days . Still want 14 months of the year 4 days * *.

there is no standard algorithm for this thing. If you have the logic in your mind, write it down bit by bit.

you can send it out and ask others to optimize it for you, shake the machine and so on. You can also look at the input and output.

my idea is year-year , and then see whether the time is big or small. If it is small, minus-1, and if it is big, it will not move.

`20180901` `20170902` `0`
`20180902` `20170901` `1`

month-month , see whether the time is big or small, and this depends on the time before processing

.
`20180901` `20171002` `010`
`20180801` `20171001` `010`
`20180901` `20171001` `011`
`20181001` `20171001` `10`
`20180902` `20170901` `10`

the number of days is the same, of course.


convert two dates to timestamps, and then subtract them.


stumbled upon a post and solved the problem code as follows

/**
 * @desc 
 * ->->
 *
 * @date 2015-09-22
 * @author WadeYu
 */

var getDiffYmdBetweenDate = function(sDate1,sDate2){
    var fixDate = function(sDate){
        var aD = sDate.split('-');
        for(var i = 0; i < aD.length; iPP){
            aD[i] = fixZero(parseInt(aD[i]));
        }
        return aD.join('-');
    };
    var fixZero = function(n){
        return n < 10 ? '0'+n : n;
    };
    var fixInt = function(a){
        for(var i = 0; i < a.length; iPP){
            a[i] = parseInt(a[i]);
        }
        return a;
    };
    var getMonthDays = function(y,m){
        var aMonthDays = [0,31,28,31,30,31,30,31,31,30,31,30,31];
        if((y%400 == 0) || (y%4==0 && y%100!=0)){
            aMonthDays[2] = 29;
        }
        return aMonthDays[m];
    };
    var checkDate = function(sDate){
    };
    var y = 0;
    var m = 0;
    var d = 0;
    var sTmp;
    var aTmp;
    sDate1 = fixDate(sDate1);
    sDate2 = fixDate(sDate2);
    if(sDate1 > sDate2){
        sTmp = sDate2;
        sDate2 = sDate1;
        sDate1 = sTmp;
    }
    var aDate1 = sDate1.split('-');
        aDate1 = fixInt(aDate1);
    var aDate2 = sDate2.split('-');
        aDate2 = fixInt(aDate2);
    //
    /*aTmp = [aDate1[0]+1,fixZero(aDate1[1]),fixZero(aDate1[2])];
    while(aTmp.join('-') <= sDate2){
        yPP;
        aTmp[0]PP;
    }*/
    y = aDate2[0] - aDate1[0];
    if( sDate2.replace(aDate2[0],'') < sDate1.replace(aDate1[0],'')){
        y = y - 1;
    }
    //
    aTmp = [aDate1[0]+y,aDate1[1],fixZero(aDate1[2])];
    while(true){
        if(aTmp[1] == 12){
            aTmp[0]PP;
            aTmp[1] = 1;
        }else{
            aTmp[1]PP;
        }
        if(([aTmp[0],fixZero(aTmp[1]),aTmp[2]]).join('-') <= sDate2){
            mPP;
        } else {
            break;
        }
    }
    //
    aTmp = [aDate1[0]+y,aDate1[1]+m,aDate1[2]];
    if(aTmp[1] > 12){
        aTmp[0]PP;
        aTmp[1] -= 12;
    }
    while(true){
        if(aTmp[2] == getMonthDays(aTmp[0],aTmp[1])){
            aTmp[1]PP;
            aTmp[2] = 1;
        } else {
            aTmp[2]PP;
        }
        sTmp = ([aTmp[0],fixZero(aTmp[1]),fixZero(aTmp[2])]).join('-');
        if(sTmp <= sDate2){
            dPP;
        } else {
            break;
        }
    }
    return {y:y,m:m,d:d};
};

var aTest = [
    ['1984-9-17','2015-8-15'],
    ['1984-9-17','1985-9-18'],
    ['1984-9-17','1984-10-17'],
    ['1984-9-17','1984-10-10'],
];
for(var i = 0; i < aTest.length; iPP){
    console.log(aTest[i][0]+':'+aTest[i][1]);
    console.log(getDiffYmdBetweenDate(aTest[i][0],aTest[i][1]));
}

Code reference: http://www.itkeyword.com/doc/.


Amway time tool

momentjs feels pretty easy to use.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a9e0-2c212.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a9e0-2c212.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?