Skip to content

Instantly share code, notes, and snippets.

@x-strong
Last active February 3, 2019 14:35
Show Gist options
  • Save x-strong/5378739 to your computer and use it in GitHub Desktop.
Save x-strong/5378739 to your computer and use it in GitHub Desktop.
"yyyy-MM-dd HH:mm:ss" format datetime regular expression.exactly match Feb‘s date no mater leap year or not. “2013-04-12 22:50:00”格式的时间正则表达式,平年闰年2月日期亦完全匹配正确。
//yyyy-MM-dd HH:mm:ss
var dateTimeRegex=/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)\s([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/;
//yyyy-MM-dd
var dateRegex=/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
//yyyyMMdd
var dateRegex2 = /^(?:(?!0000)[0-9]{4}(?:(?:0[1-9]|1[0-2])(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])(?:29|30)|(?:0[13578]|1[02])31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)0229)$/;
//HH:mm:ss
var timeRegex=/^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/;
@shinnn
Copy link

shinnn commented Jun 20, 2018

@x-strong Can I ask the license of this code? I'd like to include it in one of my applications.

@x-strong
Copy link
Author

x-strong commented Feb 3, 2019

@shinnn Yes you can : )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment