当前位置: 代码迷 >> JavaScript >> 日期检查精确到秒的js(yyyy-mm-dd-hh-mm) ,通过马上给分
  详细解决方案

日期检查精确到秒的js(yyyy-mm-dd-hh-mm) ,通过马上给分

热度:704   发布时间:2012-04-17 15:06:33.0
求一个日期检查精确到秒的js(yyyy-mm-dd-hh-mm) ,通过马上给分
格式(yyyy-mm-dd-hh-mm)

------解决方案--------------------
HTML code

<script language="javascript" type="text/javascript">
function now(){
    var today = new Date();
    var year=today.getYear();
    var month=today.getMonth()+1;
    var date=today.getDate();
    var hours=today.getHours();
    var minutes=today.getMinutes();
    var seconds=today.getSeconds();
    document.write(year+'-'+month+'-'+date+' '+hours+':'+minutes+':'+seconds)
}
now()
</script>

------解决方案--------------------
<input onblur="checkObjValue(this)">
<script language=javascript>
function check(obj)
{
var str=obj.value
var re=/^(\d{4})-(\d{2})-(\d{2})-(\d{2})-(\d{2})$/
if(!re.test(str))
return false;
var strYear=RegExp.$1;
var strMonth=RegExp.$2;
strMonth=parseInt(strMonth,10)-1
var strDate=RegExp.$3;
strDate=parseInt(strDate,10)
var strHour=RegExp.$4;
strHour=parseInt(strHour,10)
var strMinute=RegExp.$5;
strMinute=parseInt(strMinute,10)
var tempDate=new Date(strYear,strMonth,strDate,strHour,strMinute,0)
  return (tempDate.getFullYear()==strYear)&&(tempDate.getMonth()==strMonth)&&(tempDate.getDate()==strDate)&&(tempDate.getHours()==strHour)&&(tempDate.getMinutes()==strMinute)
}
function checkObjValue(obj)
{
if(check(obj))
{
alert("正确")
}else{
alert("输入错误")
obj.value=''
}
}
</script>
  相关解决方案