JS (JavaScript) custom dateadd and datediff functions and method of obtaining the current date

Source: Internet
Author: User
Tags date1

Processing date in JS is relatively junk. The following functions may help you better process the date in Js.
Including dateadd, datediff, and current date

Current date
VaR d = new date ();
VaR theday = D. getyear () + "-" + (D. getmonth (). tostring (). length> 1 )? (D. getmonth () + 1): "0" + (D. getmonth () + 1) + "-" + (D. getdate (). tostring (). length> 1 )? D. getdate (): "0" + D. getdate ());

Dateadd Function
Function dateadd (interval, num, datevalue)
{
VaR newcom = new timecom (datevalue );
Switch (string (interval). tolowercase ())
{
Case "Y": Case "year": newcom. Year + = num; break;
Case "M": Case "month": newcom. Month + = num; break;
Case "D": Case "day": newcom. day + = num; break;
Case "H": Case "Hour": newcom. Hour + = num; break;
Case "min": 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 ));
}

Timecom object
Function timecom (datevalue)
{
VaR newcom;
If (datevalue = "")
{
Newcom = new date ();
} Else {
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 ();
}

Datediff Function

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 "M ":
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 "min ":
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 );
}

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.