2017-06-13 44 views
0

這裏是我的代碼: My Business Day Calculation無法計算日期和時間追加到HTML

<input id="dDate1" type="date"> 
<input id="dDate2" type="date"> 
<label id="lbl"></label> 

    function calcBusinessDays(dDate1, dDate2) 
{ // input given as Date objects 
    var iWeeks, iDateDiff, iAdjust = 0; 
    if (dDate2 < dDate1) return -1; // error code if dates transposed 
    var iWeekday1 = dDate1.getDay(); // day of week 
    var iWeekday2 = dDate2.getDay(); 
    iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7 
    iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2; 
    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; 
    // adjustment if both days on weekend 
    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays 
    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2; 

    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000) 
    iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime())/604800000) 

    if (iWeekday1 <= iWeekday2) 
    { 
     iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1) 
    } 
    else 
    { 
     iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2) 
    } 

    iDateDiff -= iAdjust // take into account both days on weekend 

    return (iDateDiff + 1); // add 1 because dates are inclusive 
} 

var x = calcBusinessDays(new Date(document.getElementById("dDate1")), new Date(document.getElementById("dDate2"))); 
document.getElementById("lbl").innerHTML = x; 

我試圖從2個輸入字段中輸入輸入日期計算工作日。但即使我選擇了日期,標籤仍然是NaN。

謝謝你的幫助,非常感謝。

回答

0

根據您的jsfiddle link,您的第一個問題是您應該從calcBusinessDays的內部獲取日期的值,因爲您正在使用日期輸入的onblur函數(也可能會有其他解決方案)。而你的第二個問題是,在你設計你的函數調用的方式中,你必須從函數內部改變你的標籤。

有工作代碼示例:


 

 
function calcBusinessDays() 
 
{ // input given as Date objects 
 
    
 
dDate1= new Date(document.getElementById("dDate1").value); 
 
dDate2 = new Date(document.getElementById("dDate2").value); 
 
if (isNaN(dDate1) ||isNaN(dDate2)) return; 
 
    var iWeeks, iDateDiff, iAdjust = 0; 
 
    if (dDate2 < dDate1) return -1; // error code if dates transposed 
 
    var iWeekday1 = dDate1.getDay(); // day of week 
 
    var iWeekday2 = dDate2.getDay(); 
 
    iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1; // change Sunday from 0 to 7 
 
    iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2; 
 
    if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; 
 
    // adjustment if both days on weekend 
 
    iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays 
 
    iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2; 
 

 
    // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000) 
 
    iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime())/604800000) 
 

 
    if (iWeekday1 <= iWeekday2) 
 
    { 
 
     iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1) 
 
    } 
 
    else 
 
    { 
 
     iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2) 
 
    } 
 

 
    iDateDiff -= iAdjust // take into account both days on weekend 
 

 
document.getElementById("lbl").innerHTML = iDateDiff + 1; 
 
    return (iDateDiff + 1); // add 1 because dates are inclusive 
 
}
<input id="dDate1" type="date" onblur="calcBusinessDays()"> 
 
<input id="dDate2" type="date" onblur="calcBusinessDays()"> 
 
<label id="lbl"></label>

+0

它的工作!謝謝,但是如果我想排除節假日,我不知道該寫些什麼! –

+0

抱歉,但我不知道那個:(。 – pooyan