This article mainly introduces how to add days to a given time in JavaScript, involving related techniques related to date and time operations in JavaScript, for more information about how to add days in JavaScript at a given time, see the example in this article. We will share this with you for your reference. The details are as follows:
/*** Time addition processing function * @ param date the time to be calculated (xxxx-xx) * @ param plusDays the number of days to be added (integer) */function calcuDate (date, plusDays) {var dateArray = date. split ("-"); var year = + dateArray [0]; var month = + dateArray [1]; var day = + dateArray [2]; var oriDay = day; var secondMonthDays = (year % 4 = 0 & year % 100! = 0) | year % 400 = 0 )? 29: 28; while (plusDays> 0) {oriDay = day + = plusDays; switch (month) {case 4: case 6: case 9: case 11: if (day> 30) {plusDays-= (30-oriDay) + 1; month ++; day = 1 ;}else {plusDays = 0 ;}break; case 2: if (day> secondMonthDays) {plusDays-= (secondMonthDays-oriDay) + 1; month ++; day = 1;} else {plusDays = 0;} break; default: if (day> 31) {plusDays-= (31-oriDay) + 1; day = 1; month ++;} else {plusDays = 0 ;}} if (month> 12) {month = 1; year ++ ;}return createTimeString (year, month, day );}
Of course, in addition to this method, there are other solutions, such as passing parameters in new Date.