In common project development process, often encounter needs in JavaScript processing time, nothing but two (1, Logic processing 2, Format conversion processing). Of course, the relevant technology blog, the garden is closed with eyes can catch one, but I have to do is: since I have been fortunate to encounter, we should be as far as possible analysis into their best for their own things, become a part of their knowledge base, and hope to help the needy students to solve the problems encountered in the relevant small.
Time Logic Processing
Common requirements for this type are: To calculate today's date for a few months (before).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
|
* Get input date a few months ago
*{param:datetime} date Input date (YYYY-MM-DD)
Number of *{param:number} monthnum months
Getpremonthday:function (Date,monthnum)
Vardatearr = Date.split ('-');
Varyear = datearr[0];//Gets the year of the current date
Varmonth = datearr[1];//Gets the month of the current date
Varday = datearr[2];//Gets the current date of the day
Vardays =newdate (year, month, 0);
Days = Days.getdate ();//Get the number of months in the current date
Varmonth2 = parseint (month)-monthnum;
Year2 = parseint (year2)-parseint (MONTH2/12 = 0 1:parseint (month2)/12);
Month2 =-(Math.Abs (month2)% 12);
Vardays2 =newdate (year2, month2, 0);
Days2 = Days2.getdate ();
Vart2 = year2 + '-' + month2 + '-' + day2;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
|
* Get the entry date for the next one months
*{param:datetime} date Input date (YYYY-MM-DD)
Number of *{param:number} monthnum months
Getnextmonthday:function (date, monthnum)
Vardatearr = Date.split ('-');
Varyear = datearr[0];//Gets the year of the current date
Varmonth = datearr[1];//Gets the month of the current date
Varday = datearr[2];//Gets the current date of the day
Vardays =newdate (year, month, 0);
Days = Days.getdate ();//Get the number of months in the current date
Varmonth2 = parseint (month) + parseint (monthnum);
Year2 = parseint (year2) + parseint (parseint (month2)/= = 0 1:parseint (month2)/12));
Month2 = parseint (month2)% 12;
Vardays2 =newdate (year2, month2, 0);
Days2 = Days2.getdate ();
Vart2 = year2 + '-' + month2 + '-' + day2;
|
Test effect:
The hair did not find a problem, through 29th to push, before and after will be 29. January 31 to February is February 29 (because February is 29 days).
Then this logic has to be changed according to the actual situation for some requirements. For example: I would like to pay a deposit of one months. At present I have paid the 2.1-2.29 (February month), the deposit one months should be (3.1-3.31). Through this logic is 3.1-3.29, so use please according to the actual situation!
Original link: http://www.cnblogs.com/peixuanzhihou/p/6204551.html