Solved

Javascript Calculate Current Date, Plus 2 days, but skip weekends

Posted on 2009-05-08
3
942 Views
Last Modified: 2012-05-11
I have a js that works great to output a date two days from "today".  I would like it to skip weekend days. For example: If today is a Thursday, +2 days would be Saturday, but I would like the output date to be Monday, instead of Saturday.  Same thing for Fridays.

Current Script attached.
var dt = new Date();
dt.setDate(dt.getDate()+2);
var y  = dt.getYear();
if (y < 1000) y +=1900;
document.write((dt.getMonth()+1) +  "/" + dt.getDate() + "/" + y);

Open in new window

0
Comment
Question by:lexo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:wellhole
ID: 24337789
See http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_23467787.html for further information.
  //------------------------------------------------------------------
  // Name: nextWeekday()
  // Role: Determine the next weekday, remembering to skip holidays
  // Note: today is required to be a Date object
  //------------------------------------------------------------------
  function nextWeekday( today ) {
    var days = 'Sun,Mon,Tue,Wed,Thu,Fri,Sat'.split( ',' );
    var dow  = today.getDay();
    var tomorrow = new Date();
    tomorrow.setDate( tomorrow.getDate() + 1 );
    var tom  = tomorrow.getDay();
    var when = dateFormat( tomorrow );
    while ( tom < 0 || tom > 5 || holiday[ when ] ) {
      tomorrow.setDate( tomorrow.getDate() + 1 );
      tom  = tomorrow.getDay();
      when = dateFormat( tomorrow );
    }
//  alert( 'today is: ' + days[ dow ] + '\ntomorrow: ' + days[ tom ] );
    return when;
  }

Open in new window

0
 
LVL 3

Accepted Solution

by:
xbrady earned 500 total points
ID: 24337812
You can insert the following after adding the 2 days:
while(dt.getDay() == 0 || dt.getDay() == 6)
{
  dt.setDate(dt.getDate() + 1);
}
0
 

Author Closing Comment

by:lexo
ID: 31579523
Exactly what I was looking for!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Validating Date Part2 2 28
Github api to fetch the number of collaborators in a git repo 1 33
Triple line in an alert message 4 24
arrays and buttons with user input 2 34
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question