Regular Expression matching yyyy-mm-dd Date Format

Source: Internet
Author: User

In fact, I also have a headache with regular expressions. Baidu has some materials. To sum up, leave a backup here.

The question to consider is: the valid date, the number of days per month, and the leap year ....

1. valid date: the date and time between midnight on January 1, January 1, 0001 and 12:00:00 on January 1, December 31, 9999 in MSDN.

View http://msdn.microsoft.com/zh-cn/library/system.datetime (VS.80). aspx

2. Concept of a leap year: Baidu encyclopedia-four years a week, a hundred years not enough, four hundred years again

View http://baike.baidu.com/view/29649.htm

3. Integrated expression

Normal year:

The year can be written in a unified manner :(?! (0000) [0-9] {4}
The month of all years, including the Year of the same year, includes the day 1-28: (0 [1-9] | 1 [0-2]) -(0 [1-9] | 1 [0-9] | 2 [0-8])
All years, including the Year of the same year, except the Year of the same month, include the 29 and 30 days: (0 [13-9] | 1 [0-2])-(29 | 30)
All years, including year 1, 3, 5, 7, 8, 10, and December, contain 31 days: (0 [13578] | 1 [02])-31)
All the other 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)
Runnian

Year that can be divisible by 4 but cannot be divisible by 100: ([0-9] {2} (0 [48] | [2468] [048] | [13579] [26])
The year in which the object can be divisible by 400. The number that can be divisible by 400 is certainly 100, so the last two digits must be 00: (0 [48] | [2468] [048] | [13579] [26]) 00
The sum is the 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) $

For capture/non-capture of regular expressions, see: http://www.jb51.net/article/28035.htm

The last is the updated javascript method.

Copy codeThe Code is as follows: function checkDate (fname ){
Var SC = $ ("#" + fname );
Var s = SC. val ();
If (SC = null ){
Alert ("Element is null ");
Return true;
}
Var reg =/^ (? :(?! (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) $ /;
If (! S. match (reg )){
Alert ("false ");
} Else {
Alert ("true ");}

}

Copy codeThe Code is as follows: <body>
<Input type = "date" name = "textfield" id = "text1">
<Input type = "button" value = "button" onClick = "checkDate ('text1')">
</Body>

Correct Regular Expressions (including test code ): Copy codeCode: var str = '2017-12-33 ';
If (str. match (/^ ((? : 19 | 20) \ d)-(0 [1-9] | 1 [012]) -(0 [1-9] | [12] [0-9] | 3 [01]) $ /)){
Alert ('yesdate ');
} Else {
Alert ('not date ');
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.