Home  >  Article  >  Web Front-end  >  Example code for regular expression matching date and time

Example code for regular expression matching date and time

零下一度
零下一度Original
2017-05-13 10:42:522269browse

This article mainly introduces the regular expressions for JS matching date and time, and analyzes the related implementation skills of JS matching date, time, date+time and other time formats in combination with examples. Friends in need can refer to the following

The example in this article describes the regular expression of JS matching date and time. Share it with everyone for your reference, the details are as follows:

//日期的正则表达式
var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
var regExp = new RegExp(reg);
if(!regExp.test(value)){
  alert("日期格式不正确,正确格式为:2014-01-01");
  return;
}
//时间的正则表达式
var reg = /^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
var regExp = new RegExp(reg);
if(!regExp.test(value)){
  alert("时间格式不正确,正确格式为:12:00:00");
  return;
}
//日期+时间的正则表达式
var reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
var regExp = new RegExp(reg);
if(!regExp.test(value)){
  alert("时间格式不正确,正确格式为: 2014-01-01 12:00:00 ");
  return;
  }
});

[Related recommendations]

1. Special recommendation: "php Programmer Toolbox" V0.1 version download

2. Free js online video tutorial

3. php.cn Dugujiu Cheap (3) - JavaScript video tutorial

The above is the detailed content of Example code for regular expression matching date and time. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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