1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| var isLeapYear = function(year) { return (year % 4 == 0) && (year % 100 != 0 || year % 400 == 0); }
var addMonths = (function(){ var DAYS_IN_MONTH = [ [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31], [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ]; return function(date, m){ var n = date.getFullYear() * 12 + date.getMonth(); n += m; var ryear = parseInt(n / 12); var rmonth = n % 12 + 1; var rday = date.getDate();
var days_in_month = DAYS_IN_MONTH[isLeapYear(date.getFullYear()) ? 1 : 0]; if (rday > days_in_month[rmonth]){ rday = days_in_month[rmonth]; }
var d = new Date(ryear, rmonth - 1, rday); d.setHours(date.getHours()); d.setMinutes(date.getMinutes()); d.setSeconds(date.getSeconds()); d.setMilliseconds(date.getMilliseconds()); return d; } }());
|