-1

我知道这个问题被问了很多时间,但请帮助我找不到解决方案

使用下面的代码,我可以计算不包括工作日的天数

现在我还想排除公共假期,例如 2012 年 8 月 15 日、2012 年 9 月 12 日、2012 年 12 月 20 日,请帮忙

function namet()
 {

        var iWeeks, iDateDiff, iAdjust = 0;

        var nodays = document.getElementById("timestamp1").value;


        var nodays1 = document.getElementById("timestamp").value;



        var dDate1 = new Date(nodays1);



        var dDate2 = new Date(nodays);





        if (dDate2 < dDate1) {

            alert("End Date : Enter date more than Start Date ");
        }
        // 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

        var final = (iDateDiff + 1); // add 1 because dates are inclusive

        document.leaveapplication.noofdays3.value = final;
4

1 回答 1

0

您将需要一个工作日的假期列表。

  1. 计算开始和结束之间的天数
  2. 计算开始和结束之间的周末(天数模 7,然后调整最后一周的位置)
  3. 计算日期之间列表中的所有假期并减去它。
于 2012-10-30T12:15:59.880 回答