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