JS calculates the specified date plus how many days, plus how many months, plus how many years of the date

Source: Internet
Author: User

function DateAdd (interval,number,date) {/* * functionality: Implements VBScript's DATEADD functionality.   * Parameter: Interval, string expression representing the time interval to be added.   * Parameter: number, numeric expression that indicates how many time intervals to add.   * Parameters: Date, Time object.   * Returns: the 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 ())

  

JS calculates the specified date plus how many days, plus how many months, plus how many years of the date

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.