//函数名:CheckDateTime
//功能介绍:检查是否为日期时间
function CheckDateTime(str){
var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1], r[2],r[3], r[4],r[5], r[6]);
if(d.getFullYear()!=r[1])return false;
if(d.getMonth()!=r[2])return false;
if(d.getDate()!=r[3])return false;
if(d.getHours()!=r[4])return false;
if(d.getMinutes()!=r[5])return false;
if(d.getSeconds()!=r[6])return false;
return true;
}
//功能介绍:检查是否为日期时间
function CheckDateTime(str){
var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = str.match(reg);
if(r==null)return false;
r[2]=r[2]-1;
var d= new Date(r[1], r[2],r[3], r[4],r[5], r[6]);
if(d.getFullYear()!=r[1])return false;
if(d.getMonth()!=r[2])return false;
if(d.getDate()!=r[3])return false;
if(d.getHours()!=r[4])return false;
if(d.getMinutes()!=r[5])return false;
if(d.getSeconds()!=r[6])return false;
return true;
}
解决方案 »
- js读取Excel的日期格式的单元格
- js图片放大,出现了一个小问题。。。
- onMouseover和onMouseout的问题
- js里var $bud = "undefined";这个表求什么意思?
- 请问在ASP中如何接收POST方法,XMLHttpReq.send(billId)传递过来的参数
- 55555555,,,,救命啊. . . ..
- 大家看看这段代码,为什么中间要加"\"呢?
- 动态添加
- 跨域访问怎么做呢,? 访问 json格式的数据, 求教,从昨天一直研究这个,也没弄出来
- 求救,怎样在提交表单后出现一个类似安装程序的进程条,但接受服务器数据后进程条消失,显示相应数据!!!!
- IE6.0下的打印问题
- 如何取得下标由1开始的数组的长度
判断输入框中输入的日期格式为yyyy-mm-dd和正确的日期
*/
function IsDate(sm,mystring) {
var reg = /^(\d{4})-(\d{2})-(\d{2})$/;
var str = mystring;
var arr = reg.exec(str);
if (str=="") return true;
if (!reg.test(str)&&RegExp.$2<=12&&RegExp.$3<=31){
alert("请保证"+sm+"中输入的日期格式为yyyy-mm-dd或正确的日期!");
return false;
}
return true;
}
{
if (strDate.length != 8) { return null ;
}
var dtDate = null ;
var nYear = parseInt( strDate.substring( 0, 4 ), 10 ) ;
var nMonth = parseInt( strDate.substring( 4, 6 ), 10 ) ;
var nDay = parseInt( strDate.substring( 6, 8 ), 10 ) ; if( isNaN( nYear ) == true || isNaN( nMonth ) == true || isNaN( nDay ) == true )
{
return null ;
}
dtDate = new Date( nYear, nMonth - 1, nDay ) ;
if( nYear != dtDate.getFullYear() || ( nMonth - 1 ) != dtDate.getMonth() || nDay != dtDate.getDate() )
{
return null ;
} return dtDate ;
}
YYYYMMDD的格式
比如输入2000-2-29正确,1900-2-29要报错。楼上几位能达到么?
PHP中的Checkdate就直接可以判断出来。js没有相应的函数么?
如:
<script language="javascript">
<!--
function IsDate(DateString,Dilimeter)
{
if (DateString == null)
{
return false;
}
if (Dilimeter == '' || Dilimeter == null)
{
Dilimeter = '-';
}
var tempy='';
var tempm='';
var tempd='';
var tempArray;
if (DateString.length<8 || DateString.length>10)
{
return false;
} tempArray = DateString.split(Dilimeter);
if (tempArray.length != 3)
{
return false;
} if (tempArray[0].length != 4)
{
return false;
}
else
{
tempy = tempArray[0];
tempm = tempArray[1];
tempd = tempArray[2];
} if (isNaN(tempy))
{
return false;
}
if (isNaN(tempm))
{
return false;
}
if (isNaN(tempd))
{
return false;
} var tDateString = tempy + '/' + tempm + '/' + tempd;
var tempDate = new Date(tDateString); if (tempDate.getFullYear() != tempy)
{
return false;
}
if (tempDate.getMonth() != tempm-1)
{
return false;
} if (tempDate.getDate() != tempd)
{
return false;
} return true;
}
</script>