1. Technology

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

©2014 About.com. All rights reserved.