DateAdd and DateDiff Method _ time date for JavaScript date objects

Source: Internet
Author: User
Tags numeric time interval
Copy Code code as follows:

DATE.PROTOTYPE.DATEADD = function (Interval,number)
{
var d = this;
var k={' y ': ' fullyear ', ' Q ': ' Month ', ' m ': ' Month ', ' W ': ' Date ', ' d ': ' Date ', ' H ': ' Hours ', ' 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 for which the specified time interval has been added.
Dateobj.dateadd (interval, number)
Parameters
Dateobj
Required option. Any Date object.
Interval
Required option. A string expression that represents the time interval to add. For numeric values, see the "Settings" section.
Number
Required option. Numeric expression that represents the number of time intervals to add. Numeric expressions can be positive (get future dates) or negative numbers (get past dates).
DateDiff Method
Returns the time interval between two date objects.
Dateobj.datediff (interval, dateObj2)
Parameters
Interval
Required option. A string expression that is used to calculate the time interval between Date1 and Date2. For numeric values, see the "Settings" section.
Dateobj, DateObj2
Required option. The Date object. The two date objects used for the calculation.
Set up
The interval parameter can have the following values:
set description
y year
q quarterly
m month
d day
w Week
h hours
n minutes
s seconds
ms milliseconds
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.