Javascript date addition and subtraction

Source: Internet
Author: User
Tags date1
Javascript date addition and subtraction

Function timecom (datevalue)
{
VaR newcom = new date (datevalue );
This. Year = newcom. getyear ();
This. month = newcom. getmonth () + 1;
This. Day = newcom. getdate ();
This. Hour = newcom. gethours ();
This. Minute = newcom. getminutes ();
This. Second = newcom. getseconds ();
This. msecond = newcom. getmilliseconds ();
This. Week = newcom. getday ();
}

Function datediff (interval, date1, date2)
{
VaR timecom1 = new timecom (date1 );
VaR timecom2 = new timecom (date2 );
VaR result;
Switch (string (interval). tolowercase ())
{
Case "Y ":
Case "year ":
Result = timecom1.year-timecom2.year;
Break;
Case "N ":
Case "month ":
Result = (timecom1.year-timecom2.year) * 12 + (timecom1.month-timecom2.month );
Break;
Case "D ":
Case "day ":
Result = math. round (date. UTC (timecom1.year, timecom1.month-1, timecom1.day)-date. UTC (timecom2.year, timecom2.month-1, timecom2.day)/(1000*60*60*24 ));
Break;
Case "H ":
Case "Hour ":
Result = math. round (date. UTC (timecom1.year, timecom1.month-1, timecom1.day, timecom1.hour)-date. UTC (timecom2.year, timecom2.month-1, timecom2.day, timecom2.hour)/(1000*60*60 ));
Break;
Case "M ":
Case "Minute ":
Result = math. round (date. UTC (timecom1.year, timecom1.month-1, timecom1.day, timecom1.hour, timecom1.minute)-date. UTC (timecom2.year, timecom2.month-1, timecom2.day, timecom2.hour, timecom2.minute)/(1000*60 ));
Break;
Case "S ":
Case "second ":
Result = math. round (date. UTC (timecom1.year, timecom1.month-1, timecom1.day, timecom1.hour, timecom1.minute, timecom1.second)-date. UTC (timecom2.year, timecom2.month-1, timecom2.day, timecom2.hour, timecom2.minute, timecom2.second)/1000 );
Break;
Case "Ms ":
Case "msecond ":
Result = date. UTC (timecom1.year, timecom1.month-1, timecom1.day, timecom1.hour, timecom1.minute, timecom1.second, timecom1.msecond)-date. UTC (timecom2.year, timecom2.month-1, timecom2.day, timecom2.hour, timecom2.minute, timecom2.second, timecom1.msecond );
Break;
Case "W ":
Case "Week ":
Result = math. round (date. UTC (timecom1.year, timecom1.month-1, timecom1.day)-date. UTC (timecom2.year, timecom2.month-1, timecom2.day)/(1000*60*60*24) % 7;
Break;
Default:
Result = "invalid ";
}
Return (result );
}

Function dateadd (interval, num, datevalue)
{
VaR newcom = new timecom (datevalue );
Switch (string (interval). tolowercase ())
{
Case "Y": Case "year": newcom. Year + = num; break;
Case "N": Case "month": newcom. Month + = num; break;
Case "D": Case "day": newcom. day + = num; break;
Case "H": Case "Hour": newcom. Hour + = num; break;
Case "M": Case "Minute": newcom. Minute + = num; break;
Case "S": Case "second": newcom. Second + = num; break;
Case "Ms": Case "msecond": newcom. msecond + = num; break;
Case "W": Case "Week": newcom. day + = num * 7; break;
Default: Return ("invalid ");
}
VaR now = newcom. year + "/" + newcom. month + "/" + newcom. day + "" + newcom. hour + ":" + newcom. minute + ":" + newcom. second;
Return (new date (now ));
}

2. Another simple example
The date minus the number of days equals to the second date
<Script language = JavaScript>
Function CC (DD, dadd)
{
// Add error handling.
VaR A = new date (dd)
A = A. valueof ()
A = A-dadd * 24x60*60*1000
A = new date ()
Alert (A. getfullyear () + "year" + (A. getmonth () + 1) + "month" + A. getdate () + "day ")
}
CC ("12/23/2002", 2)
</SCRIPT>
I have to add some resources here, wasting a lot of time to draw lessons:
Time code of JavaScript
0-11: The number is 1-12 months. var A = new date (, 6). The result is-6.
0-6 indicates the week
1-31 represents the date
0-23 hours
0-59 minutes, seconds

 

Address: http://www.cnblogs.com/linbaba/archive/2006/12/05/583173.html

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.