JS验证日期及时间

来源:互联网 发布:java面试笔试题及答案 编辑:程序博客网 时间:2024/06/06 00:48
function checkDateTime(date){    var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;    var r = date.match(reg);    if(r == null){        alert("输入格式不正确,请按yyyy-MM-dd HH:mm:ss的格式输入!");        return false;    }else{    return true;    }        }


验证短日期(2007-06-05)function strDateTime(str){   var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);    if(r==null)return false;         var d= new Date(r[1], r[3]-1, r[4]);         return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);}验证长日期(2007-06-05 10:57:10)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[1], r[3]-1,r[4],r[5],r[6],r[7]);    return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);} 验证时间(10:57:10)function isTime(str){   var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);   if (a == null) {alert(’输入的参数不是时间格式’); return false;}   if (a[1]>24 || a[3]>60 || a[4]>60)   {       alert("时间格式不对");       return false   }    return true;}比较两个日期大小function compareDate(d1,d2){    return ((new Date(d1.replace(/-/g,"\/"))) < (new Date(d2.replace(/-/g,"\/"))));}

function CheckTime() { var checkValue = new RegExp ("^\[0-2]{1}\[0-6]{1}:\[0-5]{1}\[0-9]{1}:\[0-5]{1}\[0-9]{1}") ; var stControl = document.getElementById("txtStartTime") ; var stValue = stControl.value ; if (stValue == "") { alert("请填写开始时间") ; return false ; } if (!(checkValue.test(stValue))) { alert("请填写公司内部规定时间") ; return false ; } var etControl = document.getElementById("txtEndTime") ; var etValue = etControl.value ; if (etValue == "") { alert("请填写结束时间") ; return false ; } if (!(checkValue.test(etValue))) { alert("请填写公司内部规定时间") ; return false ; } return true ; }


0 0