javascript年月日计算不同

时间:2015-01-15 08:01:32

标签: javascript

我在同一个应用程序但不同页面计算年月日有问题。

第一页显示日期为12个月5天,而另一页显示1年0个月1天。怎么能解释这种情况呢?

<script type="text/javascript">
    function myfunc(sDate1, sDate2) { //yyyy-MM-dd
        var aDate, oDate1, oDate2, iDays;
        aDate = sDate1.split('-');
        oDate1 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]);
        aDate = sDate2.split("-");
        oDate2 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]);
        iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 / 24);

        if (iDays <= 30) {
            alert(iDays);
        } else if (iDays > 30 && iDays <= 365) {
            alert("Duration:" + parseInt((iDays / 30)) + "month(s) " + parseInt((iDays % 30)) + "day(s)")
        } else {
            alert("Duration:" + parseInt((iDays / 365)) + "year(s) " + parseInt(((iDays % 365) / 30)) + "month(s) " + parseInt(((iDays % 365) % 30)) + "day(s)");
        }

        return iDays;
    }  
</script>

My test code

第1页:

if (iDays <= 30)
                $("#Display1").html(iDays + " day(s)");
            else if (iDays > 30 && iDays <= 365)
                $("#Display1").html(parseInt(iDays / 30) + " month(s) " + (iDays % 30) + " day(s)");
            else if (iDays > 365)
                $("#Display1").html(parseInt(iDays / 365) + " year(s) " + parseInt((iDays % 365) / 30) + " month(s) " + ((iDays % 365) % 30) + " day(s)");

2页:

if (iDays <= 30)
                $("#Display2").html(iDays + " day(s)");
            else if (iDays > 30 && iDays <= 365)
                $("#Display2").html(parseInt(iDays / 30) + " month(s) " + (iDays % 30) + " day(s)");
            else if (iDays > 365)
                $("#Display2").html(parseInt(iDays / 365) + " year(s) " + parseInt((iDays % 365) / 30) + " month(s) " + ((iDays % 365) % 30) + " day(s)");

1 个答案:

答案 0 :(得分:0)

以下是MSDN的代码。我认为问题来自iDays

// Set the unit values in milliseconds.
var msecPerMinute = 1000 * 60;
var msecPerHour = msecPerMinute * 60;
var msecPerDay = msecPerHour * 24;

// Set a date and get the milliseconds
var date = new Date('6/15/1990');
var dateMsec = date.getTime();

// Set the date to January 1, at midnight, of the specified year.
date.setMonth(0);
date.setDate(1);
date.setHours(0, 0, 0, 0);

// Get the difference in milliseconds.
var interval = dateMsec - date.getTime();

// Calculate how many days the interval contains. Subtract that
// many days from the interval to determine the remainder.
var days = Math.floor(interval / msecPerDay );
interval = interval - (days * msecPerDay );

// Calculate the hours, minutes, and seconds.
var hours = Math.floor(interval / msecPerHour );
interval = interval - (hours * msecPerHour );

var minutes = Math.floor(interval / msecPerMinute );
interval = interval - (minutes * msecPerMinute );

var seconds = Math.floor(interval / 1000 );

// Display the result.
document.write(days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds.");

//Output: 164 days, 23 hours, 0 minutes, 0 seconds.

尝试使用此方法计算天数

这将是这样的

<script type="text/javascript">
function myfunc(sDate1, sDate2) { //yyyy-MM-dd
    var aDate, oDate1, oDate2, iDays;
    // Set the unit values in milliseconds.
    var msecPerMinute = 1000 * 60;
    var msecPerHour = msecPerMinute * 60;
    var msecPerDay = msecPerHour * 24;

    aDate = sDate1.split('-');
    oDate1 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]);
    aDate = sDate2.split("-");
    oDate2 = new Date(aDate[0] + '-' + aDate[1] + '-' + aDate[2]);
    var interval = oDate2.getTime() - oDate1.getTime();
    iDays =  Math.floor(interval / msecPerDay );
if(iDays <=30){
alert(oDate1);
}else if(iDays>30 && iDays <=365){
alert("Duration:" + parseInt((iDays/30)) + "month(s) " +  parseInt((iDays%30)) + "day(s)")
}else{
alert("Duration:" + parseInt((iDays/365)) + "year(s) " + parseInt(((iDays%365)/30)) + "month(s) " + parseInt(((iDays%365)%30)) + "day(s)");
}
    return iDays;

}