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

首先,我们可以在datepickers中禁用假日和周末,因为我们将在每个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;
}
Run Code Online (Sandbox Code Playgroud)
既然我们有不让用户选择周末或假期作为startdate或enddates的行为,我们继续,计算这些日期之间的总天数:
作为查找两个日期(周末除外)之间的日差异,我们有此功能来计算日期之间的工作日:
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
}
Run Code Online (Sandbox Code Playgroud)
我现在匆忙,现在你需要做的是--iDateDiff(就在返回之前)每个日期disabledDays都在dDate1和之间dDate2.
?你将如何实现这一目标?你会遍历过disabledDays数组转换/ parse它Date对象和评估,如:
var holiDay = Date.parse(iteratedDate);
if((holiDay >= dDate1 && holiDay <= dDate2)) {
--iDateDiff
}
Run Code Online (Sandbox Code Playgroud)
我希望我帮助过你......
对不起,误解了这个问题,试试这个:
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)
}
Run Code Online (Sandbox Code Playgroud)
现场演示