JS Gets the date formatted as YYYY-MM-DD for the last one months and the next one months

Source: Internet
Author: User

/** * 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.