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!