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