JavaScript to determine if date format is correct implementation code _ time Date

Source: Internet
Author: User
The reprint is at least to indicate the author and the source! Http://www.cnblogs.com/GuominQiu
Copy Code code as follows:

//---------------------------------------------------------------------------
Determine if the date format is correct
The return value is an error message and no error message indicates a valid date string
function isdatestring (strdate) {
var strseparator = "-"; Date Separator
var Strdatearray;
var intyear;
var intmonth;
var intday;
var boolleapyear;
var errormsg = ""; Error message
Strdatearray = Strdate.split (strseparator);
No judgment length, in fact 2008-8-8 is also reasonable//strdate.length!= 10 | |
if (strdatearray.length!= 3) {
ErrorMsg + = "date format must be: YYYY-MM-DD";
return errormsg;
}
Intyear = parseint (strdatearray[0],10);
Intmonth = parseint (strdatearray[1],10);
Intday = parseint (strdatearray[2],10);
if (isNaN (intyear) | | isNaN (intmonth) | | isNaN (Intday)) {
ErrorMsg + = "date format error: Year and month date must be a pure number";
return errormsg;
}
if (intmonth>12 | | intmonth<1) {
ErrorMsg + = "date format error: Month must be between 1 and 12";
return errormsg;
}
if (intmonth==1| | intmonth==3| | intmonth==5| | Intmonth==7
|| intmonth==8| |intmonth==10| | INTMONTH==12)
&& (intday>31| | intday<1)) {
ErrorMsg = = "date format error: Semi-rotary must be between 1 and 31 days";
return errormsg;
}
if (intmonth==4| | intmonth==6| | intmonth==9| | INTMONTH==11)
&& (intday>30| | intday<1)) {
ErrorMsg + = "Date format error: The number of days must be between 1 to 31";
return errormsg;
}
if (intmonth==2) {
if (Intday < 1) {
ErrorMsg + = "date format error: Date must be greater than or equal to 1";
return errormsg;
}
Boolleapyear = false;
if ((intyear%100) = = 0) {
if ((intyear%400) = = 0)
Boolleapyear = true;
}
else{
if ((intyear% 4) = = 0)
Boolleapyear = true;
}
if (boolleapyear) {
if (Intday > 29) {
ErrorMsg + = "date format error: Leap year of February days can not exceed 29";
return errormsg;
}
} else {
if (Intday > 28) {
ErrorMsg + = "date format error: Non-leap year February days cannot exceed 28";
return errormsg;
}
}
}
return errormsg;
}
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.