/** * get last one months * * @date format is YYYY-MM-DD date, such as: 2014-01-25*/functionGetpremonth (date) {vararr = Date.split ('-'); varYear = arr[0];//gets the year of the current date varmonth = arr[1];//get the month of the current date varDay = arr[2];//gets the day of the current date varDays =NewDate (year, month, 0); days= Days.getdate ();//get the number of days of the month in the current date varYEAR2 =Year ; varMonth2 = parseint (month)-1; if(Month2 = = 0) {year2= parseint (year2)-1; Month2= 12; } varDay2 =Day ; varDays2 =NewDate (year2, month2, 0); Days2=days2.getdate (); if(Day2 >days2) {Day2=Days2; } if(Month2 < 10) {month2= ' 0 ' +month2; } varT2 = year2 + '-' + month2 + '-' +Day2; returnT2;} Console.log (Getpremonth ("2014-01-25"));//2013-12-25
/** * Get next one months * * @date format is YYYY-MM-DD date, such as: 2014-01-25*/ functionGetnextmonth (date) {vararr = Date.split ('-'); varYear = arr[0];//gets the year of the current date varmonth = arr[1];//get the month of the current date varDay = arr[2];//gets the day of the current date varDays =NewDate (year, month, 0); days= Days.getdate ();//get the number of months in the current date varYEAR2 =Year ; varMonth2 = parseint (month) + 1; if(Month2 = = 13) {year2= parseint (year2) + 1; Month2= 1; } varDay2 =Day ; varDays2 =NewDate (year2, month2, 0); Days2=days2.getdate (); if(Day2 >days2) {Day2=Days2; } if(Month2 < 10) {month2= ' 0 ' +month2; } varT2 = year2 + '-' + month2 + '-' +Day2; returnT2;} Console.log (Getnextmonth ("2014-10-31"));//2014-11-30
JS Gets the date formatted as YYYY-MM-DD for the last one months and the next one months