Date added in JavaScript program code

Source: Internet
Author: User
Tags getdate time interval

Instance

I summarized the following, wrote a JS method, the main solution: a date + how many months days = a date of the problem. As for, a date-a date = how many days

The code is as follows Copy Code

Solve the situation that the time is nan under IE
function parseISO8601 (datestringinrange) {
var isoexp =/^s* (D{4})-(DD)-(DD) s*$/,
Date = new Date (NaN), month,
Parts = isoexp.exec (Datestringinrange);
if (parts) {
month = +parts[2];
Date.setfullyear (Parts[1], month-1, parts[3]);
if (month!= date.getmonth () + 1) {
Date.settime (NaN);
}
}
return date;
}
var change_date = function (dates,months,days) {
var datestr = parseISO8601 (dates);
Parameter represents the number of days to increase under the current date
var now = new Date (DATESTR);
if (months!= 0) {
Now.setmonth (Now.getmonth () + months);
}
Now.setdate ((Now.getdate ()-1) + 1 * days);
var year = Now.getfullyear ();
if (months!= 0) {
var month = Now.getmonth () +1;
}else{
var month = Now.getmonth () +1;
}
var day = Now.getdate ();
if (Month < 10) {
month = ' 0 ' + month;
}
if (Day < 10) {
Day = ' 0 ' + day;
}
Return year + '-' + month + '-' + day ';
};
var n = change_date ("2012-12-01", 12,32);
alert (n);


The addition and subtraction of dates in JavaScript. Like today + 80 Days

Example

The code is as follows Copy Code

<script>
function DateAdd (interval,number,date)
{
/*
*---------------DateAdd (interval,number,date)-----------------
* DATEADD (Interval,number,date)
* Function: To achieve the DATEADD function of VBScript.
* 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>

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.