The simplest regular expression is:/d {4}-/d {2}-/d {2}
However, the actual situation is not that simple. You should consider the validity and leap years .....
The effective range of dates varies in different application scenarios. 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 UNIX timestamp 0 follows the ISO 8601 standard: 1970-01-01T00: 00: 00Z.
First, consider the first three rules irrelevant to the year. The year can be written in a unified manner.
(?! (0000) [0-9] {4}
Here we only consider the monthly and daily regular expressions.
1. The month of all years, including the normal year, contains 1-28 days.
(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8])
2. All the years, including the Year of the Year, including the year of the first month, include the 29 and 30 days.
(0 [13-9] | 1 [0-2])-(29 | 30)
3. All years, including the Year of the Year, including the year 1, 3, 5, 7, 8, 10, and the year of the month, all contain the 31
(0 [0, 13578] | 1 [02])-31)
In combination, it is all the dates except the February 29 of the 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 we will consider the implementation of the leap year.
1: four-year-old
([0-9] {2} (0 [48] | [2468] [048] | [13579] [26])
2: Never renew for four hundred years, and never renew for years
(0 [48] | [2468] [048] | [13579] [26]) 00
3: Combined is the year February 29 of all leap years
([0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [13579] [26]) 00)-02-29)
All the four rules have been implemented and have no influence on each other. The combination is the regular expression of all dates that conform to 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, it is meaningless to capture a group. It only occupies resources and affects matching efficiency. Therefore, you can use a non-capture group 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 [1, 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 day-to-day verification
This js date Regular Expression supports day-to-day verification. We will give an example of a function instance for date verification expressions.
Function isdate (str ){
Var result = str. match (/^ (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 (d. getFullYear () = result [1] & d. getMonth () + 1 = result [3] & d. getDate () = result [4]);
}
Check if it is a YYYY-MM-DD | YYYY/MM/DD Date Format
Only monthly and daily regular expressions are considered.
1. The month of all years, including the normal year, contains 1-28 days.
(0 [1-9] | 1 [0-2])-(0 [1-9] | 1 [0-9] | 2 [0-8])
2. All the years, including the Year of the Year, including the year of the first month, include the 29 and 30 days.
(0 [13-9] | 1 [0-2])-(29 | 30)
3. All years, including the Year of the Year, including the year 1, 3, 5, 7, 8, 10, and the year of the month, all contain the 31
(0 [0, 13578] | 1 [02])-31)
In combination, it is all the dates except the February 29 of the 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 = str. match (/^ (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 (d. getFullYear () = result [1] & (d. getMonth () + 1) = result [3] & d. getDate () = result [4] & d. getHours () = result [5] & d. getMinutes () = result [6] & d. getSeconds () = result [7]);
}
// Determine whether the input is in valid long date format-"YYYY-MM-DD HH: MM: SS" | "YYYY/MM/dd hh: MM: SS"
This is from yyyy-mm-dd hh: mm: ss.
/^ (D {4})-(d {2})-(d {2}) (d {2}) :( d {2}) :( d {2 }) $ /;
This is from yyyy-mm-ddde.
/^ (D {4})-(d {2})-(d {2}) $/