Dateadd and datediff for JS

Source: Internet
Author: User
Paste the Javascript I wrote first Code For more information, see.


/* Get the date after the date, month, day, and other plus numbers */

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;

}

/* Calculate the date, year, month, and day of the two dates */

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. ArbitraryDateObject.

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 for calculationDate1AndDate2Time interval. For values, see the "Settings" section.

Dateobj, dateobj2

Required. Date object. Two date objects used for calculation.

Set

IntervalParameters can have the following values:

& Lt; th width = "75%" & gt; description & lt;/th & gt;
Settings
Y year
q quarter
m month
d day
W week
H hour
n minute
S seconds
MS millisecond

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.