js calculate the time difference between the two in January

"Constraints: End Time endTime> startTime Start Time

"Ideas: Before always miss out on a variety of situations, so include a variety of circumstances, found that the law:

1.年-月:(endTime.getYear()-startTime.getYear())*12

2. May - May: endTime.getMonth () - startTime.getMonth (), which happens a negative situation

3. Day - May: This example points to a specific time, first in terms of hours and minutes all day long, and then to monthly statistics.

    If endTime.getDate ()> = startTime.getDate (), or 0, otherwise -1

"Code

 1 function monDiff(startTime,endTime){
 2     startTime=new Date(startTime);
 3     endTime=new Date(endTime);
 4     var date2Mon;
 5      var startDate=startTime.getDate()+startTime.getHours()/24+startTime.getMinutes()/24/60;
 6     var endDate=endTime.getDate()+endTime.getHours()/24+endTime.getMinutes()/24/60;
 7     if(endDate>=startDate){
 8         date2Mon=0;
 9     }else{
10         date2Mon=-1;
11     }
12     return (endTime.getYear-startTime.getYear())*12+endTime.getMonth-startTime.getMonth()+date2Mon;
13 }

 

Guess you like

Origin www.cnblogs.com/Demetris/p/11531990.html