JavaScript version DateAdd and DateDiff function code _javascript tips

Source: Internet
Author: User
Tags date1 getdate

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>

Related Article

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.