Armik Armik - 5 months ago 29
Javascript Question

Future Date Displayer Excluding Weekends

I'm not great with Javascript, however I've found the following code to calculate a future date depending on the figure entered. However the problem I have is that it includes weekends. Is there some code I can add to this existing Javascript that allows it to skip weekends?

For example, if the date lands on a Saturday or Sunday, the date that is displayed is for the Monday?

<script>
var myDelayInDays=2, myDate=new Date();
myDate.setDate(myDate.getDate()+myDelayInDays);
document.write('In 2 Days it will be: ' + myDate.toLocaleDateString());
</script>

RJN RJN
Answer

You can do this in a number of ways, it's really just what you prefer. A simple way would be,

<script>
  var myDelayInDays=2, myDate=new Date();
  myDate.setDate(myDate.getDate()+myDelayInDays);

  if(myDate.getDay() == 0){//Sunday
    myDate.setDate(myDate.getDate() + 1);//Monday
  }
  else if(myDate.getDay() == 6){//Saturday
    myDate.setDate(myDate.getDate() + 2);//Monday
  }
  document.write('In 2 Days it will be: ' + myDate.toLocaleDateString());
</script>