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 );
}