Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function NewDate (str ){
Str = str. split ('-');
Var date = new Date ();
Date. setUTCFullYear (str [0], str [1]-1, str [2]);
Date. setUTCHours (0, 0, 0, 0 );
Return date;
}
Function TimeCom (dateValue ){
Var newCom;
If (dateValue = ""){
NewCom = new Date ();
} Else {
NewCom = NewDate (dateValue );
}
This. year = newCom. getYear ();
This. month = newCom. getMonth () + 1;
This. day = newCom. getDate ();
This. hour = newCom. getHours ();
This. minute = newCom. getMinutes ();
This. second = newCom. getSeconds ();
This. msecond = newCom. getMilliseconds ();
This. week = newCom. getDay ();
}
Function DateDiff (interval, date1, date2 ){
Var TimeCom1 = new TimeCom (date1 );
Var TimeCom2 = new TimeCom (date2 );
Var result;
Switch (String (interval). toLowerCase ()){
Case "y ":
Case "year ":
Result = TimeCom1.year-TimeCom2.year;
Break;
Case "m ":
Case "month ":
Result = (TimeCom1.year-TimeCom2.year) * 12 + (TimeCom1.month-TimeCom2.month );
Break;
Case "d ":
Case "day ":
Result = Math. round (Date. UTC (TimeCom1.year, TimeCom1.month-1, TimeCom1.day)-Date. UTC (TimeCom2.year, TimeCom2.month-1, TimeCom2.day)/(1000*60*60*24 ));
Break;
Case "h ":
Case "hour ":
Result = Math. round (Date. UTC (TimeCom1.year, TimeCom1.month-1, TimeCom1.day, TimeCom1.hour)-Date. UTC (TimeCom2.year, TimeCom2.month-1, TimeCom2.day, TimeCom2.hour)/(1000*60*60 ));
Break;
Case "min ":
Case "minute ":
Result = Math. round (Date. UTC (TimeCom1.year, TimeCom1.month-1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute)-Date. UTC (TimeCom2.year, TimeCom2.month-1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute)/(1000*60 ));
Break;
Case "s ":
Case "second ":
Result = Math. round (Date. UTC (TimeCom1.year, TimeCom1.month-1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second)-Date. UTC (TimeCom2.year, TimeCom2.month-1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second)/1000 );
Break;
Case "ms ":
Case "msecond ":
Result = Date. UTC (TimeCom1.year, TimeCom1.month-1, TimeCom1.day, TimeCom1.hour, TimeCom1.minute, TimeCom1.second, TimeCom1.msecond)-Date. UTC (TimeCom2.year, TimeCom2.month-1, TimeCom2.day, TimeCom2.hour, TimeCom2.minute, TimeCom2.second, TimeCom1.msecond );
Break;
Case "w ":
Case "week ":
Result = Math. round (Date. UTC (TimeCom1.year, TimeCom1.month-1, TimeCom1.day)-Date. UTC (TimeCom2.year, TimeCom2.month-1, TimeCom2.day)/(1000*60*60*24) % 7;
Break;
Default:
Result = "invalid ";
}
Return (result );
}
</Script>