1. Home
  2. Computing & Technology
  3. JavaScript

Adding Days to a Date

Join the Discussion

Questions? Comments?

Adding or subtracting a number of days from a date is another common requirement that doesn't have a built in method on the date function in order to simplify the process for us (not that it is that hard to do once you know how to do it).

Rather than having to remember how to add or subtract a number of days from a date we wan simplify the process by adding our own addDays() method to all our date objects allowing us to simply pass the number of days we want to add to the date in order to automatically change the date. By passing a negative number the same method will allow us to subtract days from a date.

Date.prototype.addDays = function(days) {
this.setDate(this.getDate()+days);
}
Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. JavaScript

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

All rights reserved.