未来日期计算器



我有以下脚本,当使用时,允许用户查看未来的日期,同时排除周末。我遇到的问题是,如果当前日期是星期五,我将未来日期设置为 3 天,它将星期六和星期日计为工作日。我真的希望你们中的一个人能够提供帮助,因为我在Javascript方面并不是那么出色。

正确的示例是:如果今天 = 星期五,那么从现在起的 3 个工作日将是星期三(而不是脚本当前计算的星期一(。

有什么想法吗?

          var myDelayInDays = 3; 
          myDate=new Date();
          myDate.setDate(myDate.getDate()+myDelayInDays);
          if(myDate.getDay() == 0){//Sunday
            myDate.setDate(myDate.getDate() + 2);//Tuesday
          } else if(myDate.getDay() == 6){//Saturday
            myDate.setDate(myDate.getDate() + 2);//Monday
          }
          document.write('' + myDate.toLocaleDateString('en-GB'));

任何帮助都会很棒。谢谢

通过更改要添加的日期和日期来尝试此代码, 自定义循环用于跳过周六和周日

function addDates(startDate,noOfDaysToAdd){
  var 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++;
    }
  }
  return startDate;
}
var today = new Date();
var daysToAdd = 3;
alert(addDates(today,daysToAdd));

最新更新