/** * Get last one months * * @date format is YYYY-MM-DD date, such as: 2014-01-25 */function Getpremonth (d ATE) {var arr = date.split ('-'); var year = arr[0]; Gets the year of the current date var month = arr[1]; Gets the month of the current date var day = arr[2]; Gets the day of the current date var days = new Date (year, month, 0); Days = Days.getdate (); Gets the number of days of the month in the current date var year2 = year; var month2 = parseint (month)-1; if (month2 = = 0) {year2 = parseint (year2)-1; Month2 = 12; } var day2 = day; var days2 = new Date (year2, month2, 0); Days2 = Days2.getdate (); if (Day2 > Days2) {day2 = Days2; } if (Month2<Ten) {month2= ' 0 '+ month2; } var T2= Year2+ '-' + month2 + '-' + day2; return T2; /** * Get Next one months * * @date format is YYYY-MM-DD date, such as: 2014-01-25 */FU Nction Getnextmonth (date) {var arr= Date.split ('-'); var year= arr[0];//Gets the year of the current date var month= arr[1];//Get the Month Var day for the current date= arr[2];//Gets the day of the current date var days= newDate (year, month, 0); days= Days.getdate ();//Gets the number of months in the current date var year2= year; var month2= parseint (month)+ 1; if (Month2== {year2= parseint (year2)+ 1; Month2= 1; } var day2= day; var days2= newDate (year2, month2, 0); Days2= Days2.getdate (); if (day2>days2) {day2 = Days2; } if (Month2<Ten) {month2= ' 0 '+ month2; } var T2= Year2+ '-' + month2 + '-' + day2; return T2; }//Test//alert (Getpremonth ("2014-01-25")); Alert (Getnextmonth ("2014-12-25"));
Source: http://www.cnblogs.com/love540376/p/4582443.html
/** * Get last one months * * @date format is YYYY-MM-DD date, such as: 2014-01-25 */function Getpremonth (date) {var arr = date.split ('-'); var year = arr[0]; Gets the year of the current date var month = arr[1]; Gets the month of the current date var day = arr[2]; Gets the day of the current date var days = new Date (year, month, 0); Days = Days.getdate (); Gets the number of days of the month in the current date var year2 = year; var month2 = parseint (month)-1; if (month2 = = 0) {year2 = parseint (year2)-1; Month2 = 12; } var day2 = day; var days2 = new Date (year2, month2, 0); Days2 = Days2.getdate (); if (Day2 > Days2) {day2 = Days2; } if (Month2 <) {month2 = ' 0 ' + month2; } var t2 = year2 + '-' + month2 + '-' + day2; return T2; }/** * Get next one months * * @date format yyYy-mm-dd date, such as: 2014-01-25 */function Getnextmonth (date) {var arr = date.split ('-'); var year = arr[0]; Gets the year of the current date var month = arr[1]; Gets the month of the current date var day = arr[2]; Gets the day of the current date var days = new Date (year, month, 0); Days = Days.getdate (); Gets the number of days of the month in the current date var year2 = year; var month2 = parseint (month) + 1; if (month2 = =) {year2 = parseint (year2) + 1; Month2 = 1; } var day2 = day; var days2 = new Date (year2, month2, 0); Days2 = Days2.getdate (); if (Day2 > Days2) {day2 = Days2; } if (Month2 <) {month2 = ' 0 ' + month2; } var t2 = year2 + '-' + month2 + '-' + day2; return T2; }//Test//alert (Getpremonth ("2014-01-25")); Alert (Getnextmonth ("2014-12-25 "));
JS get last one month, next month