简体   繁体   English

未来日期计算器

[英]Future Date Calculator

I have the following script that when used, allows the user to see a future date, while excluding weekends. 我具有以下脚本,该脚本在使用时允许用户在不包括周末的情况下查看将来的日期。 The problem i've encountered though is if the current day is Friday, and i set the future date to 3 days it counts the Saturday and Sunday as working days. 我遇到的问题是当前日期是星期五,而我将将来的日期设置为3天,它将星期六和星期日算作工作日。 I'm really hoping one of you may be able to help as I'm not really that great at Javascript. 我真的希望你们中的一个能够提供帮助,因为我对Javascript的了解不是那么出色。

The correct example would be: If Today = Friday then 3 working days from now would be Wednesday (not Monday as the script currently calculates it). 正确的示例是:如果今天=星期五,则从现在起3个工作日将是星期三(而不是脚本当前计算的星期一)。

Any ideas? 有任何想法吗?

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

Any help would really be great. 任何帮助真的很棒。 Thanks 谢谢

Try this code by changing date and days to add, A custom loop is used to skip sat and sun 尝试通过更改日期和日期来添加此代码,使用自定义循环跳过星期六和星期日

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM