This article transferred from: http://stackoverflow.com/questions/5645058/how-to-add-months-to-a-date-in-javascript/5645126
I took a look at the Datejs and stripped out the code necessary to add months to a date handling edge cases (leap year, SH Orter months, etc):
date.isleapyear = function (year) {return ((year% 4 = = = 0) && (year% 100!== 0)) | | (Year% 400 = = = 0)); };D Ate.getdaysinmonth = function (year, month) {return [Date.isleapyear] (year) 29:28), 31, 30, 31, 30, 31, 31, 31][month],;D Ate.prototype.isLeapYear = function () {return date.isleapyear (This.getfullyear ());};D Ate.prototype.getDaysInMonth = function () {return date.getdaysinmonth (This.getfullyear (), This.getmonth ());};D Ate.prototype.addMonths = function (value) {var n = this.getdate (); This.setdate (1); This.setmonth (This.getmonth () + value); This.setdate (Math.min (n, This.getdaysinmonth ())); return this;};
This would add "addMonths ()" function to any JavaScript date object that should handle edge cases. Thanks to Coolite inc!
Use:
var myDate = new Date("01/31/2012");var result1 = myDate.addMonths(1);var myDate2 = new Date("01/31/2011");var result2 = myDate2.addMonths(1);
->> Newdate.addmonths-Mydate.addmonths
RESULT1 = "Feb 29 2012"
RESULT2 = "Feb 28 2011"
[Go]js add month plus n month