How JavaScript handles a tutorial for a specified date a few months ago or months later

Source: Internet
Author: User
Tags datetime getdate knowledge base

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
VARYEAR2 = year;
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);
Days2 = Days2.getdate ();
if (Day2 > Days2) {
Day2 = Days2;
}
if (Month2 < 10) {
Month2 = ' 0 ' + month2;
}
Vart2 = year2 + '-' + month2 + '-' + day2;
Returnt2;
}
?
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
VARYEAR2 = year;
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);
Days2 = Days2.getdate ();
if (Day2 > Days2) {
Day2 = Days2;
}
if (Month2 < 10) {
Month2 = ' 0 ' + month2;
}
Vart2 = year2 + '-' + month2 + '-' + day2;
Returnt2;
}

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.