DateAdd function:
Copy Code code as follows:
function DateAdd (interval,number,date) {
Switch (Interval.tolowercase ()) {
Case "Y": Return new Date (Date.setfullyear (Date.getfullyear () +number));
Case "M": Return new Date (Date.setmonth (Date.getmonth () +number));
Case "D": Return new Date (Date.setdate (Date.getdate () +number));
Case "W": Return new Date (Date.setdate (Date.getdate () +7*number));
Case "H": Return new Date (Date.sethours (date.gethours () +number));
Case "n": Return new Date (Date.setminutes (Date.getminutes () +number));
Case "S": Return new Date (Date.setseconds (Date.getseconds () +number));
Case "L": return new Date (Date.setmilliseconds (Date.getmilliseconds () +number));
}
}
DateDiff function:
Copy Code code as follows:
function DateDiff (interval,date1,date2) {
var long = Date2.gettime ()-date1.gettime (); Millisecond difference
Switch (Interval.tolowercase ()) {
Case "Y": Return parseint (Date2.getfullyear ()-date1.getfullyear ());
Case "M": Return parseint (Date2.getfullyear ()-date1.getfullyear ()) *12 + (Date2.getmonth ()-date1.getmonth ());
Case "D": Return parseint (LONG/1000/60/60/24);
Case "W": Return parseint (LONG/1000/60/60/24/7);
Case "H": Return parseint (LONG/1000/60/60);
Case "n": Return parseint (LONG/1000/60);
Case "S": Return parseint (long/1000);
Case "L": return parseint (Long);
}
}
DATEDIFF functions compatible with multiple browsers
Copy Code code 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) * + (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>