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