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 |