Home  >  Article  >  Web Front-end  >  JavaScript implementation code to determine whether the date format is correct_time and date

JavaScript implementation code to determine whether the date format is correct_time and date

WBOY
WBOYOriginal
2016-05-16 18:05:121351browse

The reprinter should at least indicate the author and source! http://www.cnblogs.com/GuominQiu

Copy code The code is as follows:

/ /------------------------------------------------- --------------------------
//Determine whether the date format is correct
//The return value is an error message, no error message That is to say, the legal 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 length judgment, in fact 2008-8-8 It is also reasonable //strDate.length != 10 ||
if(strDateArray.length != 3) {
ErrorMsg = "The 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, month and day must be pure numbers";
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: the number of days in the big month must be between 1 and 31";
return ErrorMsg;
}
if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)
&&(intDay>30||intDay<1)) {
ErrorMsg = "Date format Error: The number of days in a small month must be between 1 and 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 0) == 0){
if((intYear@0) == 0)
boolLeapYear = true;
}
else{
if((intYear % 4) == 0)
boolLeapYear = true;
}
if(boolLeapYear){
if(intDay > 29) {
ErrorMsg = "Date format error: the number of days in February in leap years cannot exceed 29";
return ErrorMsg;
}
} else {
if(intDay > 28) {
ErrorMsg = "Date format error: the number of days in February in non-leap years cannot exceed 28";
return ErrorMsg;
}
}
}
return ErrorMsg;
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn