Home > Article > Web Front-end > How to determine whether the input string is in date format
下面小编就为大家带来一篇js判断输入的字符串是否是日期格式的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
实例如下所示:
function isDate(dateString){ if(dateString.trim()=="")return true; var r=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"); return false; } var d=new Date(r[1],r[3]-1,r[4]); var num = (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); }
The above is the detailed content of How to determine whether the input string is in date format. For more information, please follow other related articles on the PHP Chinese website!