javascriptは2つの日付差の関数コードを計算します.

1500 ワード


    //         yyyy-mm-dd
    function curDateTime() {
        var d = new Date();
        var year = d.getFullYear();
        var month = d.getMonth() + 1;
        var date = d.getDate();
        var day = d.getDay();
        var ms = d.getMilliseconds();
        var curDateTime = year;
        if (month > 9)
            curDateTime = curDateTime + "-" + month;
        else
            curDateTime = curDateTime + "-0" + month;
        if (date > 9)
            curDateTime = curDateTime + "-" + date;
        else
            curDateTime = curDateTime + "-0" + date;
        return curDateTime;
    }
    //                YYYY-MM-dd
    function daysBetween(DateOne, DateTwo){
        var OneMonth = DateOne.substring(5, DateOne.lastIndexOf('-'));
        var OneDay = DateOne.substring(DateOne.length, DateOne.lastIndexOf('-') + 1);
        var OneYear = DateOne.substring(0, DateOne.indexOf('-'));
        var TwoMonth = DateTwo.substring(5, DateTwo.lastIndexOf('-'));
        var TwoDay = DateTwo.substring(DateTwo.length, DateTwo.lastIndexOf('-') + 1);
        var TwoYear = DateTwo.substring(0, DateTwo.indexOf('-'));
        var cha = ((Date.parse(OneMonth + '/' + OneDay + '/' + OneYear) - Date.parse(TwoMonth + '/' + TwoDay + '/' + TwoYear)) / 86400000);
        return Math.abs(cha); //           ,    -     ,  return cha;
    }
    document.write(daysBetween(curDateTime(), '2010-11-17')); //