欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript 判斷日期格式是否正確的實現(xiàn)代碼

 更新時間:2011年07月04日 23:42:41   作者:  
沒有多大變動,主要是返回錯誤信息,以便調(diào)用函數(shù)部分可以alert出來。據(jù)說可以用正則表達式校驗,下次再研究下。
轉(zhuǎn)載者最起碼注明作者和出處!http://www.cnblogs.com/GuominQiu
復(fù)制代碼 代碼如下:

//---------------------------------------------------------------------------
//判斷日期格式是否正確
//返回值是錯誤信息, 無錯誤信息即表示合法日期字符串
function isDateString(strDate){
var strSeparator = "-"; //日期分隔符
var strDateArray;
var intYear;
var intMonth;
var intDay;
var boolLeapYear;
var ErrorMsg = ""; //出錯信息
strDateArray = strDate.split(strSeparator);
//沒有判斷長度,其實2008-8-8也是合理的//strDate.length != 10 ||
if(strDateArray.length != 3) {
ErrorMsg += "日期格式必須為: 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 += "日期格式錯誤: 年月日必須為純數(shù)字";
return ErrorMsg;
}
if(intMonth>12 || intMonth<1) {
ErrorMsg += "日期格式錯誤: 月份必須介于1和12之間";
return ErrorMsg;
}
if((intMonth==1||intMonth==3||intMonth==5||intMonth==7
||intMonth==8||intMonth==10||intMonth==12)
&&(intDay>31||intDay<1)) {
ErrorMsg += "日期格式錯誤: 大月的天數(shù)必須介于1到31之間";
return ErrorMsg;
}
if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)
&&(intDay>30||intDay<1)) {
ErrorMsg += "日期格式錯誤: 小月的天數(shù)必須介于1到31之間";
return ErrorMsg;
}
if(intMonth==2){
if(intDay < 1) {
ErrorMsg += "日期格式錯誤: 日期必須大于或等于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 += "日期格式錯誤: 閏年的2月份天數(shù)不能超過29";
return ErrorMsg;
}
} else {
if(intDay > 28) {
ErrorMsg += "日期格式錯誤: 非閏年的2月份天數(shù)不能超過28";
return ErrorMsg;
}
}
}
return ErrorMsg;
}

相關(guān)文章

最新評論