如何获取开始日期和结束日期之间的日期? (周末除外)

时间:2010-08-26 03:35:28

标签: javascript jquery

您希望获得开始日期和结束日期之间的日期列表。例如,开始日期是27-08-2010,结束日期是31-08-2010。所以日期清单是27-08-2010,30-08-2010和31-08-2010。 29-08-2010和30-08-2010将被忽略,因为它是在周末。我附上图片以获得更清晰的解释。如何使用javascript或jquery实现这一目标?我只想获得已经完成的工作日计算的日期列表。

alt text

1 个答案:

答案 0 :(得分:5)

首先,我们可以在datepickers中禁用假期和周末,因为我们将在每个datepicker组件中将其设置为:

从DatePicker

禁用周末和假日
var disabledDays = ["10-22-2010","8-16-2010"];
$("#startDate").datepicker({
    constrainInput: true,
beforeShowDay: noWeekendsOrHolidays
});

function nationalDays(date) {
    var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
    for (i = 0; i < disabledDays.length; i++) {
        if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
            return [false];
        }
    }
    return [true];
}
function noWeekendsOrHolidays(date) {
    var noWeekend = jQuery.datepicker.noWeekends(date);
    return noWeekend[0] ? nationalDays(date) : noWeekend;
}

现在我们的行为是不让用户选择周末或假期作为startdate或enddates,我们继续,计算这些日期之间的总天数:

计算两个日期之间的营业日期

as Find day difference between two dates (excluding weekend days)我们有这个函数来计算日期之间的工作日:

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
}

倒计时假期

我现在匆忙,你现在需要做的是--iDateDiff(就在返回之前)disabledDaysdDate1和{{dDate2之间的每个日期1}}。

您将如何实现这一目标?...您将迭代disabledDays数组并将其/ parse转换为日期对象并进行评估:

var holiDay = Date.parse(iteratedDate);
if((holiDay >= dDate1 && holiDay <= dDate2)) {
    --iDateDiff
}

我希望我帮助过你......


EDITED:

抱歉,误解了这个问题,试试这个:

var Weekday = new Array("Sun","Mon","Tue","Wed","Thuy","Fri","Sat");
while (startDate<=endDate)
  {
  var weekDay = startDate.getDay();
  if (weekDay < 6 && weekDay > 0) {
    var month = startDate.getMonth()+1;
    if( month <= 9 ) { month = "0"+month; }
    var day = startDate.getDate();
    if( day <= 9 ) { day = "0"+day; }
    document.write(day+"/"+month+"/"+startDate.getFullYear() + " ("+Weekday[weekDay]+")<br />");
  }
  startDate.setDate(startDate.getDate()+1)

  }

直播 DEMO

相关问题