if(dateString.trim()=="")returntrue
varr=dateString.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/)
if(r==null){
alert("请输入格式正确的日期\n\r日期格式:yyyy-mm-dd\n\r例 如:2008-08-08\n\r")
returnfalse
}
vard=newDate(r[1],r[3]-1,r[4])
varnum = (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])
if(num==0){
alert("请输入格式正确的日期\n\r日期格式:yyyy-mm-dd\n\r例 如:2008-08-08\n\r")
}
return(num!=0)
}
代码如下:
/*日期验证dd/mm/yyyy*/
functionisDate(str){
vardate=str
varresult=date.match(/^([0]?[1-9]|[1|2][0-9]|[3][0|1])[/]([0]?[1-9]|[1][0-2])[/]([0-9]{4}|[0-9]{2})$/)
if(result==null)
returnfalse
vard=newDate(result[3],result[2]-1,result[1])
return(d.getFullYear()==result[3]&&(d.getMonth()+1)==result[2]&&d.getDate()==result[1])
}
扩展资料
java用正则表达式判断输入的日期格式为yyyy/MM/dd
publicstaticvoidmain(String[]args)throwsParseException{
Stringstr1="2012-05-15"
Stringstr2="2012/05/15"
if(zzbd(str1)){
System.out.println("str1havematch!")
}else{System.out.println("str1don'tmatch!")}
if(zzbd(str2)){
System.out.println("str2havematch!")
}else{System.out.println("str2don'tmatch!")}}
publicstaticbooleanzzbd(Strings)
{
Patternpattern=Pattern.compile(\\d{4}/\\d{2}/\\d{2})
Matchermatcher=pattern.matcher(s)
if(matcher.find())
{
returntrue
}else{
returnfalse
}
}
}
function strToDate(str) {var tempStrs = str.split(" ")
var dateStrs = tempStrs[0].split("-")
var year = parseInt(dateStrs[0], 10)
var month = parseInt(dateStrs[1], 10) - 1
var day = parseInt(dateStrs[2], 10)
var timeStrs = tempStrs[1].split("-")
var hour = parseInt(timeStrs [0], 10)
var minute = parseInt(timeStrs[1], 10) - 1
var second = parseInt(timeStrs[2], 10)
var date = new Date(year, month, day, hour, minute, second)
return date
}