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

Source: Internet
Author: User
Tags getdate 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 ago 3 *{param:datetime} Date Input Date (YYYY-MM-DD) 4 *{param:number} monthnum months 5 */6 Getpremonthday:function (Date,monthnum) 7 {8 var Datearr = date.split ('-'); 9 var year = Datearr [0]; Gets the year of the current date, var month = datearr[1]; Gets the month of the current date one var day = datearr[2]; Gets the day of the current date var days = new Date (year, month, 0); Gets the number of days in the current date in the month var year2 = year;15 var month2 = parseint (month)-monthnum;16 if (month2 <=0) {year2 = parseint (year2)-parseint (MONTH2/12 = = 0? 1:parseint (month2)/each); month2 = 1         2-(Math.Abs (month2)%),}20 var day2 = day;21 var days2 = new Date (year2, month2, 0); 22 Days2 = Days2.getdate (), if (Day2 > Days2) {day2 = days2;25}26 if (month 2 <) {month2 = ' 0 ' + month2;28}29 var t2 = yEar2 + '-' + month2 + '-' + day2;30 return t2;31} 
 1/** 2 * Get the input date for the next one months 3 *{param:datetime} date Input Date (YYYY-MM-DD) 4 *{param:number} monthnum months 5 */6 Getnextmonthday:function (date, Monthnum) 7 {8 var Datearr = date.split ('-'); 9 var year = Datear R[0]; Gets the year of the current date, var month = datearr[1]; Gets the month of the current date one var day = datearr[2]; Gets the day of the current date var days = new Date (year, month, 0); Gets the number of days in the current date of the month var year2 = year;15 var month2 = parseint (month) + parseint (monthnum);              H2 >12) {year2 = parseint (year2) + parseint ((parseint (month2)/= = 0? 1:parseint (month2)/12)); 18 Month2 = parseint (month2)% 12;19}20 var day2 = day;21 var days2 = new Date (YEAR2, M         Onth2, 0); days2 = Days2.getdate (); if (Day2 > Days2) {day2 = days2;25}26 if (Month2 <) {month2 = ' 0 ' + month2;28}29 30 var t2 = year2 + '-' + month2 + '-' + day2;31 return t2;32} 

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

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.