Code of the DateAdd and DateDiff functions in JavaScript

Source: Internet
Author: User
Tags date1

DateAdd function:

Copy codeCode: 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 codeThe Code is 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 browsersCopy 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>

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.