添加否。 在约会的日期(周末除外)

我有个约会,我需要加上否。 获取未来日期的天数,但应排除周末。 即

input date = "9-DEC-2011"; No. of days to add = '13'; next date should be "28-Dec-2011" 

这里周末(坐/太阳)不计算在内。

试试这个

 var startDate = "9-DEC-2011"; startDate = new Date(startDate.replace(/-/g, "/")); var endDate = "", noOfDaysToAdd = 13, count = 0; while(count < noOfDaysToAdd){ endDate = new Date(startDate.setDate(startDate.getDate() + 1)); if(endDate.getDay() != 0 && endDate.getDay() != 6){ //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday) count++; } } alert(endDate);//You can format this date as per your requirement 

工作演示

@ShankarSangoli

这是一个较新的版本,它避免在每个循环上重新创建一个Date对象,请注意它现在包含在一个函数中。

 function calcWorkingDays(fromDate, days) { var count = 0; while (count < days) { fromDate.setDate(fromDate.getDate() + 1); if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends count++; } return fromDate; } alert(calcWorkingDays(new Date("9/DEC/2011"), 13)); 

如果您只想计算工作日,那么这个function对您有帮助。

 function countWorkingDays(fromDate, days) { var count = 0; while (count < days) { fromDate.setDate(fromDate.getDate() + 1); if (fromDate.getDay() != 0 && fromDate.getDay() != 6) // Skip weekends count++; } } countWorkingDays('06-01-2017',10); 

如果你想在工作时间以上工作而不是评论并给我你的问题,那么我会尝试解决这个问题。

或者你可以这样

 function addWeekdays(date, weekdays) { var newDate = new Date(date.getTime()); var i = 0; while (i < weekdays) { newDate.setDate(newDate.getDate() + 1); var day = newDate.getDay(); if (day > 1 && day < 7) { i++; } } return newDate; } var currentDate = new Date('10/31/2014'); var targetDate = addWeekdays(currentDate, 45); alert(targetDate); 

这个问题已经过时了,但之前的所有答案都在逐一迭代。 这可能是很多天的低效率。 这适用于我,假设days是一个正int并且startDate是一个工作日:

 function addWorkingDates(startDate, days) { current_day = startDate.getDay() - 1; // Week day, starting on Monday weekend_days = 2*parseInt((current_day + days)/5); startDate.setDate(changed_to.getDate() + days + weekend_days); } 

这是一个优雅的解决方案,没有任何循环或外部库:

 function addBusinessDaysToDate(date, days) { var day = date.getDay(); date = new Date(date.getTime()); date.setDate(date.getDate() + days + (day === 6 ? 2 : +!day) + (Math.floor((days - 1 + (day % 6 || 1)) / 5) * 2)); return date; } var date = "9-DEC-2011"; var newDate = addBusinessDaysToDate(new Date(date.replace(/-/g, "/")), 13); alert(newDate.toString().replace(/\S+\s(\S+)\s(\d+)\s(\d+)\s.*/, '$2-$1-$3')); // alerts "28-Dec-2011" 

试试这个解决方案

  

如果您想从特定日期开始下一个工作日,请使用以下代码…

 function getNextWorkingDay(originalDate) { var nextWorkingDayFound = false; var nextWorkingDate = new Date(); var dateCounter = 1; while (!nextWorkingDayFound) { nextWorkingDate.setDate(originalDate.getDate() + dateCounter); dateCounter++; if (!isDateOnWeekend(nextWorkingDate)) { nextWorkingDayFound = true; } } return nextWorkingDate; } function isDateOnWeekend(date) { if (date.getDay() === 6 || date.getDay() === 0) return true; else return false; } 

出于某种原因,我更直观地尝试递归。 此版本不考虑假期,但您可以更改isValid函数以检查任何内容。

 function addWeekdaysToDate(date, numberToAdd) { var isValid = function(d) { return d.getDay() !== 0 && d.getDay() !== 6 } if(Math.abs(numberToAdd) > 1) { return addWeekdaysToDate( addWeekdaysToDate(date, Math.sign(numberToAdd)), numberToAdd - Math.sign(numberToAdd) ) } else if(Math.abs(numberToAdd) === 1) { var result = new Date(date) result.setDate(result.getDate() + Math.sign(numberToAdd)) if(isValid(result)) { return result } else { return addWeekdaysToDate(result, Math.sign(numberToAdd)) } } else if(numberToAdd === 0) { return date } return false } console.log(addWeekdaysToDate(new Date(), 1)) console.log(addWeekdaysToDate(new Date(), 5)) console.log(addWeekdaysToDate(new Date(), -7)) 

试试这个

      
Date of book delivery: