The specified date after a few months or months before the JavaScript time is processed

Source: Internet
Author: User
Tags knowledge base

In the common project development process, often encounter the need to deal with the time in JavaScript, nothing more than two (1, Logic processing 2, Format conversion processing). Of course, the relevant technology Bo, the garden can be caught with eyes closed, but I have to do is: since the fortunate I met, it is necessary to transform as much as possible to their own things, become a part of their knowledge base, but also hope to help needy students to solve the related small problems encountered.

Time Logic processing

Common requirements for this type are: Calculate today's date after a few months (ago).

1     /**2 * Get input date a few months ago3 *{param:datetime} date Input dates (YYYY-MM-DD)4 *{param:number} monthnum number of months5     */6Getpremonthday:function(Date,monthnum)7     {8         varDatearr = Date.split ('-'));9         varYear = datearr[0];//gets the year of the current dateTen         varmonth = datearr[1];//get the month of the current date One         varDay = datearr[2];//gets the day of the current date A         varDays =NewDate (year, month, 0); -Days = Days.getdate ();//get the number of days of the month in the current date -         varYEAR2 =Year ; the         varMonth2 = parseint (month)-Monthnum; -         if(Month2 <=0) { -Year2 = parseint (year2)-parseint (MONTH2/12 = = 0? 1:parseint (month2)/12); -Month2 =-(Math.Abs (month2)% 12); +         } -         varDay2 =Day ; +         varDays2 =NewDate (year2, month2, 0); ADays2 =days2.getdate (); at         if(Day2 >days2) { -Day2 =Days2; -         } -         if(Month2 < 10) { -Month2 = ' 0 ' +month2; -         } in         varT2 = year2 + '-' + month2 + '-' +Day2; -         returnT2; to}
1      /**2 * Get input dates for the next one months3 *{param:datetime} date input dates (YYYY-MM-DD) 4 *{param:number} monthnum number of months5     */6Getnextmonthday:function(date, monthnum)7     {8         varDatearr = Date.split ('-'));9         varYear = datearr[0];//gets the year of the current dateTen         varmonth = datearr[1];//get the month of the current date One         varDay = datearr[2];//gets the day of the current date A         varDays =NewDate (year, month, 0); -Days = Days.getdate ();//get the number of months in the current date -         varYEAR2 =Year ; the         varMonth2 = parseint (month) +parseint (monthnum); -         if(Month2 >12) { -Year2 = parseint (year2) + parseint ((parseint (month2)/= = 0? 1:parseint (month2)/12)); -Month2 = parseint (month2)% 12; +         } -         varDay2 =Day ; +         varDays2 =NewDate (year2, month2, 0); ADays2 =days2.getdate (); at         if(Day2 >days2) { -Day2 =Days2; -         } -         if(Month2 < 10) { -Month2 = ' 0 ' +month2; -         } in  -         varT2 = year2 + '-' + month2 + '-' +Day2; to         returnT2; +}

Test results:

Hair did not find a problem, through 29th to push, before and after will be 29. January 31 pushed to February is February 29 (because February is 29 days).

Then this logic will have to change according to the actual situation for some needs. For example: I would like to pay for the one-month deposit. At the moment, I have paid the 2.1-2.29 (month of February), and it should be (3.1-3.31) one months in advance. This logic gets but is 3.1-3.29, so use please according to the actual situation!

  

PS: Others bo always solve the needs of others. Meet the new requirements and update it .......

The specified date after a few months or months after the JavaScript time is processed

Related Article

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.