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

时间:2011-12-09 20:15:48

标签: javascript jquery

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

input date = "9-DEC-2011";
No. of days to add  = '13';

next date should be "28-Dec-2011"

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

10 个答案:

答案 0 :(得分:22)

试试这个

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

正在使用 Demo

答案 1 :(得分:5)

@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));

答案 2 :(得分:2)

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

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"

答案 3 :(得分:1)

或者你可以这样

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);

答案 4 :(得分:1)

这个问题已经很老了,但之前的所有答案都在逐一迭代。这可能是很多天的低效率。这对我有用,假设from Tkinter import * def get_Data(): text_from_Box = Text_Entry.get("1.0", 'end-1c').split("\n") print text_from_Box master = Tk() Label(master, text = "Enter coordinates here:").grid(row = 0, sticky = W) Text_Entry = Text(master, height = 30, width = 30) Text_Entry.grid(row = 1, column = 0) Button(master, text = 'Start Calculation', command = get_Data).grid(row = 2, column = 0, sticky = W) mainloop() 为正整数且days为工作日:

startDate

答案 5 :(得分:0)

尝试此解决方案

<script language="javascript">

function getDateExcludeWeekends(startDay, startMonth, startYear, daysToAdd) {
var sdate = new Date();
var edate = new Date();
var dayMilliseconds = 1000 * 60 * 60 * 24;
sdate.setFullYear(startYear,startMonth,startDay);
edate.setFullYear(startYear,startMonth,startDay+daysToAdd);
var weekendDays = 0;
while (sdate <= edate) {
    var day = sdate.getDay()
    if (day == 0 || day == 6) {
        weekendDays++;
    }
    sdate = new Date(+sdate + dayMilliseconds);
} 
sdate.setFullYear(startYear,startMonth,startDay + weekendDays+daysToAdd);
return sdate;
}

</script>

答案 6 :(得分:0)

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

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;
}

答案 7 :(得分:0)

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

&#13;
&#13;
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))
&#13;
&#13;
&#13;

certain browsers中,您可能需要Math.sign的填充:

Math.sign = Math.sign || function(x) {
  x = +x; // convert to a number
  if (x === 0 || isNaN(x)) {
    return Number(x);
  }
  return x > 0 ? 1 : -1;
}

答案 8 :(得分:0)

  

2019

这个问题已经很老了,已经有了答案,但是我相信现在有很多人使用矩量库来处理日期时间。因此,这是我使用矩型库实现此目标的解决方案:

对于使用moment的用户:

const DATE_FORMAT = 'DD-MMM-YYYY';

function addDays(datestring, numberOfDays) {
  const date = moment(datestring, DATE_FORMAT);
  // Weekend occurrence * 2 days
  const weekendCounts = Math.floor(numberOfDays / 5) * 2;
  // If it is Friday, add additional 2 days to skip the weekend
  if (date.day() === 5) {
    return date.add(numberOfDays + weekendCounts + 2, 'days');
  }
  // If it is Saturday, add additional 1 day to skip the weekend
  else if (date.day() === 6) {
    return date.add(numberOfDays + weekendCounts + 1, 'days');
  }
  // Otherwise, add the number of days
  return date.add(numberOfDays + weekendCounts, 'days');
}

addDays('9-DEC-2011', 13); // 28-DEC-2011

答案 9 :(得分:-1)

试试这个

<html>
<head>


<script type="text/javascript">
function calculate(){
var noOfDaysToAdd = 13;
var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = ""; 
count = 0;
while(count < noOfDaysToAdd){
endDate = new Date(startDate.setDate(startDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6)
{
count++;
}
}
document.getElementById("result").innerHTML = endDate;
}
</script>
</head>
<body>
<div>
Date of book delivery: <span id="result"></span><br /><br />
<input type="button" onclick="calculate();" value="Calculate"/>
<br>
<br>
</div>
</body>
</html>