JS calculates the date of the specified date plus the number of days, plus the number of months, plus the number of years

Source: Internet
Author: User
Tags getdate
function DateAdd (interval,number,date) {/* * Feature: Enables VBScript DATEADD functionality.
  * Parameter: interval, a string expression representing the time interval to add.
  * Parameter: number, numeric expression, representing the amount of time interval to add.
  * 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
}} var now = new Date ();
Plus five days.
var newdate = DateAdd ("D", 5,now);
Alert (newdate.tolocaledatestring ())//plus two months.
Newdate = DateAdd ("M", 2,now);
Alert (newdate.tolocaledatestring ())//plus one year newdate = DateAdd ("y", 1,now);  Alert (newdate.tolocaledatestring ())

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.