<script language=javascript>
function strDateTime(str){
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r, r-1,r);
var newStr=d.getFullYear()+r+(d.getMonth()+1)+r+d.getDate()
return newStr==str
}
alert(strDateTime("2002-1-31"))
alert(strDateTime("2002-1-41"))
</script>
<script language=javascript>
function strDateTime(str){
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
var d= new Date(r, r-1,r,r,r,r);
var newStr=d.getFullYear()+r+(d.getMonth()+1)+r+d.getDate()+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds()
return newStr==str
}
alert(strDateTime("2002-1-31 12:34:56"))
alert(strDateTime("2001-2-29 12:54:56"))
alert(strDateTime("2002-1-41 12:00:00"))
</script>
<script language="javascript">
var s="2002-4-16"
alert(chkDate(s));
function chkDate(sDate){
var r=/\d{4}(?:-\d{1,2}){0,2}/
//正则表达式,判断是否为yyyy-mm-dd,yyyy-mm,yyyy格式
if(sDate.match(r)==sDate){
var arr=sDate.split("-")
switch(arr.length){
//根据不同的yyyy-mm-dd,yyyy-mm格式判断年月日数字是否正确
case 3:
var tmpDate=new Date(arr[0],arr,arr);
if(tmpDate.getMonth()==arr && tmpDate.getFullYear()==arr[0]) return true;
break;
case 2:
if(arr<13) return true;
break;
default:
return false;
}
}
return false;
}
</script>
/*
时间有效性判断函数
All by happywinds
*/
function verifyDate(textObj) {
var str=textObj.value;
textObj.value = textObj.value.replace(/\s+/g,"");
if(str.search(/^\d{4}-\d{1,2}-\d{1,2}$/) == 0){
var y = parseInt(str.split("-")[0]);
var m = parseInt(str.split("-"));
var d = parseInt(str.split("-"));
switch(m){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if(d>31){
return false;
textObj.focus();
textObj.select();
}else{
return true;
}
break;
case 2:
if((y%4==0 && d>29) || ((y%4!=0 && d>28))){
return false;
textObj.focus();
textObj.select();
}else{
return true;
}
break;
case 4:
case 6:
case 9:
case 11:
if(d>30){
return false;
textObj.focus();
textObj.select();
}else{
return true;
}
break;
default:
return false;
textObj.focus();
textObj.select();
}
}else{
return false;
textObj.focus();
textObj.select();
}
}