1. Home
  2. Computing & Technology
  3. JavaScript
Stephen Chapman
Stephen's JavaScript Blog

By Stephen Chapman, About.com Guide to JavaScript

Add Business Days to a Date

Tuesday June 30, 2009
Adding methods to existing objects in JavaScript makes even complex tasks simple

You dont need a complicated loop to be able to skip over the weekends when adding or subtracting business days. A half dozen lines of code in a method added to your existing date objects will quickly and efficiently add the functionality without needing a loop.

Add Business Days to a Date
Comments
July 2, 2009 at 9:58 am
(1) aaron says:

Hello – thanks for this! But it seems using (-5) brings you to the same date…?

i.e.
var aDate = new Date(”7/23/2009″);
aDate.addBusDays(-5);

so…

aDate.getMonth() + 1 + “/” + aDate.getDate() + “/” + aDate.getFullYear() = “7/23/2009″

July 2, 2009 at 3:09 pm
(2) Stephen says:

Oops, a slight error in the script – I have now corrected it.

July 2, 2009 at 4:15 pm
(3) aaron says:

Ah, I see… I was close; I added the dys < 0 as an else if.

That fixed the 5s starting on a weekday, but when using a Sat or Sun as a start date, the 5s are off…?

i.e.
var aDate = new Date(”7/25/2009″);
aDate.addBusDays(-5) gives you 7/19.

July 3, 2009 at 5:16 pm
(4) Stephen says:

A few flaws in the original script so I have now completely rewritten it.

Thanks for identifying those special cases I missed testing for first time around.

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.