Common date functions in Javascript

Source: Internet
Author: User
Tags date1

 

// Determine whether it is a date

// Note: you cannot accurately determine the date of January 1, February in a leap year. js must be used for additional processing.

Function isdate (STR)
{
VaR patt =/^ ([1-9]) | (0 [1-9]) | (1 [012]) [-. /] (0? /D) | ([12]/d) | (3 [01]) [-. /] (19 | 20)/d {2}) | (19 | 20)/d {2 }[-. /] ([1-9]) | (0 [1-9]) | (1 [012]) [-. /] (0? /D) | ([12]/d) | (3 [01]) $ /;
Return patt. Test (STR );
}

// Return the system date and time

// Reference http://blog.csdn.net/avon520/archive/2008/06/13/2544133.aspx

Function getsystemdatetime ()
{
VaR now = new date ();
// Date
VaR yyyy = now. getfullyear (). tostring ();
VaR month = now. getmonth () + 1; // The month must be calculated with a plus of 1.
VaR Mm = month. tostring (). Length = 1? "0" + month. tostring (): month. tostring ();
VaR dd = now. getdate (). tostring (). Length = 1? "0" + now. getdate (). tostring (): Now. getdate (). tostring ();
// Time
VaR H = now. gethours (). tostring ();
VaR M = now. getminutes (). tostring ();
VaR S = now. getseconds (). tostring ();

Return mm + "/" + dd + "/" + yyyy + "" + H + ":" + M + ":" + S; //
}

 

 

// Date Calculation

// Returns the number of days of the date1-date2

// Note: the two dates are directly subtracted, And the return value is the difference in milliseconds.

Function datediff (date1, date2)
{
If (date1> date2)
Return parseint (math. Abs (date1-date2)/1000/60/60/24 );
Else
Return 0-parseint (math. Abs (date2-date1)/1000/60/60/24 );
}

 

// Returns the number of days of the specified year/month.

Function daysinmonth (Imonth, iyear)
{
VaR days = 0;
If (Imonth = 2)
{
If (iyear % 4 = 0 & (iyear % 100! = 0 | iyear % 400 = 0 ))
Days = 29;
Else
Days = 28;
}
Else
{
Days = 32-new date (-- Imonth, Imonth, 32). getdate ();
}

Return days;
}

 

// Tostringformat (formatstring)

Date. Prototype. tostringformat = function (formatstring)
{
// Date
VaR yyyy = This. getfullyear (). tostring ();
VaR M = (this. getmonth () + 1). tostring ();
VaR Mm = M. Length = 1? "0" + M: m;
VaR d = This. getdate (). tostring ();
VaR dd = D. Length = 1? "0" + D: D;
// Time
VaR H = This. gethours (). tostring ();
VaR HH = H. Length = 1? "0" + H: h;
VaR M = This. getminutes (). tostring ();
VaR Mm = M. Length = 1? "0" + M: m;
VaR S = This. getseconds (). tostring ();
VaR Ss = S. Length = 1? "0" + S: S;
Formatstring = formatstring. Replace (/YYYY/g, yyyy );
Formatstring = formatstring. Replace (/MM/g, mm );
Formatstring = formatstring. Replace (/DD/g, DD );
Formatstring = formatstring. Replace (/hh/g, HH );
Formatstring = formatstring. Replace (/MM/g, mm );
Formatstring = formatstring. Replace (/SS/g, SS );
Formatstring = formatstring. Replace (/M/G, M );
Formatstring = formatstring. Replace (/D/g, d );
Formatstring = formatstring. Replace (/h/g, h );
Formatstring = formatstring. Replace (/M/G, M );
Formatstring = formatstring. Replace (/S/g, S );
Return formatstring;
}

 

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.