Home > Web Front-end > JS Tutorial > How to determine whether the input string is in date format

How to determine whether the input string is in date format

怪我咯
Release: 2017-06-29 10:23:27
Original
3497 people have browsed it

下面小编就为大家带来一篇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);
 }
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template