DateAdd and dateDiff methods of Javascript date objects

Source: Internet
Author: User

Copy codeThe Code is as follows: Date. prototype. dateAdd = function (interval, number)
{
Var d = this;
Var k = {'y': 'fullyear ', 'q': 'month', 'M': 'month', 'w': 'date', 'D ': 'date', 'H': 'urs', 'N': 'minutes ', 's': 'seconds', 'ms': 'milliseconds '};
Var n = {'q': 3, 'w': 7 };
Eval ('d. set '+ k [interval] +' (d. get '+ k [interval] +' () + '+ (n [interval] | 1) * number) + ')');
Return d;
}
Date. prototype. dateDiff = function (interval, objDate2)
{
Var d = this, I ={}, t = d. getTime (), t2 = objDate2.getTime ();
I ['y'] = objDate2.getFullYear ()-d. getFullYear ();
I ['q'] = I ['y'] * 4 + Math. floor (objDate2.getMonth ()/4)-Math. floor (d. getMonth ()/4 );
I ['M'] = I ['y'] * 12 + objDate2.getMonth ()-d. getMonth ();
I ['ms'] = objDate2.getTime ()-d. getTime ();
I ['W'] = Math. floor (t2 + 345600000)/(604800000)-Math. floor (t + 345600000)/(604800000 ));
I ['D'] = Math. floor (t2/86400000)-Math. floor (t/86400000 );
I ['H'] = Math. floor (t2/3600000)-Math. floor (t/3600000 );
I ['n'] = Math. floor (t2/60000)-Math. floor (t/60000 );
I ['s '] = Math. floor (t2/1000)-Math. floor (t/1000 );
Return I [interval];
}

DateAdd Method
Returns the date object with the specified interval added.
DateObj. dateAdd (interval, number)
Parameters
DateObj
Required. Any Date object.
Interval
Required. String expression, indicating the time interval to be added. For values, see the "Settings" section.
Number
Required. Numeric expression, indicating the number of time intervals to be added. A numeric expression can be a positive number (to get the future date) or a negative number (to get the past date ).
DateDiff Method
Returns the time interval between two date objects.
DateObj. dateDiff (interval, dateObj2)
Parameters
Interval
Required. String expression used to calculate the time interval between date1 and date2. For values, see the "Settings" section.
DateObj, dateObj2
Required. Date object. Two date objects used for calculation.
Set
The interval parameter can have the following values:

Set Description
Y Year
Q Quarter
M Month
D Day
W Week
H Hours
N Minutes
S Seconds
MS Millisecond
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.