判断日期是否正确?

wise1979 wise1979 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
以下函数在判断录入的日期数据是否正确时,在录入其它月份时没有问题,不知道为什么在录入8月份和9月份时,例如:2009-8-32或者2009-9-32时不能返回true,请高手帮忙看一下,谢谢!

//检查判断输入的日期格式是否正确//
function CheckDate(INDate){
if (INDate==""){return true;}
//转换年份//
subYY = INDate.substr(0,4);
if(isNaN(subYY) || subYY<=0){return true;}
//转换月份//
if(INDate.indexOf('-',0)!=-1){separate="-"}
else{
if(INDate.indexOf('/',0)!=-1){separate="/"}
else {return true;}
}
area=INDate.indexOf(separate,0)
subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))
if(isNaN(subMM) || subMM<=0){
return true;
}
if(subMM.length<2){subMM="0"+subMM}
//转换日//
area=INDate.lastIndexOf(separate)
subDD=INDate.substr(area+1,INDate.length-area-1)
if(isNaN(subDD) || subDD<=0){
return true;
}
if(eval(subDD)<10){subDD="0"+eval(subDD)}
NewDate=subYY+"-"+subMM+"-"+subDD
if(NewDate.length!=10){return true;}
if(NewDate.substr(4,1)!="-"){return true;}
if(NewDate.substr(7,1)!="-"){return true;}
var MM=NewDate.substr(5,2);
var DD=NewDate.substr(8,2);
if(parseInt(MM)>12){return true;}//月份大于12
if((subYY%4==0 && subYY%100!=0)||subYY%400==0){ //判断是否为闰年
if(parseInt(MM)==2){
if(DD>29){return true;}
}
}else{
if(parseInt(MM)==2){
if(DD>28){return true;}
}
}
//根据月份判断最大天数
if(parseInt(MM)==1){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==3){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==5){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==7){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==8){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==10){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==12){if(parseInt(DD)>31){return true;}}
if(parseInt(MM)==2){if(parseInt(DD)>30){return true;}}
if(parseInt(MM)==4){if(parseInt(DD)>30){return true;}}
if(parseInt(MM)==6){if(parseInt(DD)>30){return true;}}
if(parseInt(MM)==9){if(parseInt(DD)>30){return true;}}
if(parseInt(MM)==11){if(parseInt(DD)>30){return true;}}
//以上判断完毕后返回false值
return false;
}
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
wise1979
ASP 28,401 成员 356,940 内容