JS code for DATEADD and DATEDIFF functions

Source: Internet
Author: User
Tags date1

DateAdd function:

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:

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);
}
}

JS code for DATEADD and DATEDIFF functions

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.