JavaScript implementation date by month add and subtract _javascript tips

Source: Internet
Author: User
Tags getdate time interval

You need to use it in your project and write one yourself. JavaScript date is reduced by month

<! DOCTYPE html>  

Method Two:

<script language= "JavaScript" > Date.prototype.Format = function (FMT) {//Code author: meizz var o = {"m+": This.getmo Nth () + 1,//month "d+": this.getdate (),//day "h+": this.gethours (),//hr "m+": this.getminutes (),//min "s+": this.gets 
Econds (),//sec "q+": Math.floor (This.getmonth () + 3)/3),//Quarter "S": this.getmilliseconds ()//millisecond}; 
if (/(y+)/.test (FMT)) FMT = Fmt.replace (regexp.$1, (this.getfullyear () + ""). substr (4-regexp.$1.length)); For (var k in O) if (new RegExp ("+ K +")). Test (FMT)) FMT = Fmt.replace (regexp.$1, (regexp.$1.length = 1)? 
(O[k]): (("+ o[k]"). substr (("" + o[k). length)); 
return FMT;
} Date.prototype.addDays = function (d) {this.setdate (this.getdate () + D);};
Date.prototype.addWeeks = function (w) {this.adddays (w * 7); date.prototype.addmonths= function (m) {var d = this.getdate (); This.setmonth (This.getmonth () + m); if (This.getdate () ;
d) this.setdate (0);
}; Date.prototype.addYears = function (y) {var m = this.getmonth (); thiS.setfullyear (This.getfullyear () + y);
if (M < This.getmonth ()) {this.setdate (0);}}; </script>

Method Three:

<script> function DateAdd (interval,number,date) {/* *---------------DateAdd (interval,number,date)------- 
 ----------* DATEADD (interval,number,date) * Function: To achieve VBScript DateAdd function. 
 * 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); * AUTHOR:WANGHR100 (Grey bean baby. NET) * update:2004-5-28 11:46 *---------------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 ()) </script>

The above is the entire contents of this article, I hope you can enjoy.

Related Article

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.