The simplest regularity is as follows: /d{4}-/d{2}-/d{2}
But the actual situation is not that simple, we need to consider issues such as validity and leap years...
Different application scenarios will vary for the valid range of dates. The valid range of the DateTime object defined in MSDN is: 0001-01-01 00:00:00 to 9999-12-31 23:59:59.
The 0 of UNIX timestamp is as follows: 1970-01-01T00:00:00Z according to ISO 8601 specification.
First consider the first three rules that are not related to the year, and the year can be written in a unified manner.
(?!0000)[0-9]{4}
The following is the following rules for month and day
1. The months of all years including the average year include 1-28 days
(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])
2. All years including the average year include 29 and 30 days except February
(0[13-9]|1[0-2])-(29|30)
3. All years including the common year include 1, 3, 5, 7, 8, 10, and December.
(0[13578]|1[02])-31)
Together, it is all other dates except February 29 of a leap year
(?!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)
Next, consider the implementation of leap years
1: Four years every leap
([0-9]{2}(0[48]|[2468][048]|[13579][26])
2: If there is no leap for a hundred years, it will be another leap for a hundred years.
(0[48]|[2468][048]|[13579][26])00
3: Together, it will be February 29th of all leap years
([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)
All four rules have been implemented and have no effect on each other. Together, they are the regular rules for all dates that meet the DateTime range.
^((?!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)$
Considering that this regular expression is only used for verification, the capture group has no meaning and will only occupy resources and affect the matching efficiency, so non-capture groups can be used for optimization.
^(?:(?!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)$
Category: JavaScript
js date regular expression supports month-day verification
This js date regular expression supports month-date verification. We will give examples of function instances about date verification expressions.
function isdate(str){
var result=(/^(d{4})(-|/)(d{1,2})2(d{1,2})$/);
if(result==null) return false;
var d=new Date(result[1], result[3]-1, result[4]);
return (()==result[1] && ()+1==result[3] && ()==result[4]);
}
Check if it is YYYY-MM-DD || YYYY/MM/DD date format
The face only considers the regularity of the month and day
1. The months of all years including the average year include 1-28 days
(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])
2. All years including the average year include 29 and 30 days except February
(0[13-9]|1[0-2])-(29|30)
3. All years including the common year include 1, 3, 5, 7, 8, 10, and December.
(0[13578]|1[02])-31)
Together, it is all other dates except February 29 of a leap year
(?!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)
function isdatetime(str)
{
var result=(/^(d{4})(-|/)(d{1,2})2(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2})$/);
if(result==null) return false;
var d= new Date(result[1], result[3]-1, result[4], result[5], result[6], result[7]);
return (()==result[1]&&(()+1)==result[3]&&()==result[4]&&()==result[5]&&()==result[6]&&()==result[7]);
}
// Determine whether the input is valid long date format - "YYYY-MM-DD HH:MM:SS" || "YYYY/MM/DD HH:MM:SS"
This is yyyy-mm-dd hh:mm:ss
/^(d{4})-(d{2})-(d{2}) (d{2}):(d{2}):(d{2})$/ ;
This is yyyy-mm-ddde's
/^(d{4})-(d{2})-(d{2})$/