Use js to calculate the difference of several months between two months

Source: Internet
Author: User

Project requirements: calculate the number of months between two months. If the number is less than, the calculation is based on one month. That is, the period from January 1 to January 2 is two months, and the period from January 1 to January 1 is one month, find some information from the Internet, join your own needs, and write your own js method, which is provided for your reference. For example, the start date is 20110801 and the end date is 20110902.
Java code
Function setRateValidity (){
Var beginDate = document. getElementById ("crlimit: beginDate"). value;
Var endDate = document. getElementById ("crlimit: endDate"). value;
If (beginDate! = "" & EndDate! = "" & EndDate> = beginDate & (endDate-beginDate) <= 10000 ){
Var startDate = new Date (beginDate. substr (), beginDate. substr ()-1, beginDate. substr (); // The month starts from 0.
Var startDateUTC = Date. UTC (startDate. getFullYear (), startDate. getMonth (), startDate. getDate ());
Var end = new Date (endDate. substr (), endDate. substr ()-1, endDate. substr ));
Var endDateUTC = Date. UTC (end. getFullYear (), end. getMonth (), end. getDate (); var workmonthnum = 0;
For (var I = startDateUTC; I <= endDateUTC; I = I + 86400000 ){
Var days = new Date (I );
If (days. getDate () = 1 ){
Workmonthnum ++;
}
}
If (beginDate. substr (6, 2) = '01' & endDate. substr (6, 2) = '01 '){
Workmonthnum = workmonthnum-1;
}
If (beginDate. substr () = endDate. substr () & beginDate. substr () = endDate. substr )){
Workmonthnum = 1;
}
Document. getElementById ("crlimit: rateValidity"). value = workmonthnum;
}
}

Author "weidongke123-126-com"
 

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.