(JS) specifies the date plus day, month, and year.

Source: Internet
Author: User

VaR dt = new date ();
// Add a day.
VaR newdate = dateadd ("D", 5, DT );
Alert (newdate. tolocaledatestring ())
// Add a month.
Newdate = dateadd ("M", 2, now );
Alert (newdate. tolocaledatestring ())
// Add year
Newdate = dateadd ("Y", 1, now );
Alert (newdate. tolocaledatestring ())

Function dateadd (interval, number, date)
{
/*
* Function: implements the dateadd function of VBScript.
* Parameter: interval, a string expression, indicating the time interval to be added.
* Parameter: Number, numeric expression, indicating the number of time intervals to be added.
* Parameter: date, time object.
* Return: a new time object.
* Var now = new date ();
* Var newdate = dateadd ("D", 5, now );
* --------------- Dateadd (interval, number, date )-----------------
*/
Switch (interval)
{
Case "Y ":
Date. setfullyear (date. getfullyear () + number );
Return date;
Break;
Case "Q ":
Date. setmonth (date. getmonth () + number * 3 );
Return date;
Break;
Case "M ":
Date. setmonth (date. getmonth () + number );
Return date;
Break;
Case "W ":
Date. setdate (date. getdate () + number * 7 );
Return date;
Break;
Case "D ":
Date. setdate (date. getdate () + number );
Return date;
Break;
Case "H ":
Date. sethours (date. gethours () + number );
Return date;
Break;
Case "M ":
Date. setminutes (date. getminutes () + number );
Return date;
Break;
Case "S ":
Date. setseconds (date. getseconds () + number );
Return date;
Break;
Default:
Date. setdate (D. getdate () + number );
Return date;
Break;
}
}

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.