Date format Validation

Source: Internet
Author: User

Date format YYYYMMDD Regular expression: var reg =/^ ([0-9]{3}[1-9]|[ 0-9]{2}[1-9][0-9]| [0-9] [1-9] [0-9] {2}| [1-9] [0-9] {3}) ((0[13578]|10|12) (0[1-9]|1[0-9]|2[0-9]|3[01]) | (0[469]|11) (0[1-9]|1[0-9]|2[0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]) | (((0[48]| [13579] [26]| [2468] [048]) 000209) | ([0-9]{2} (0[48]|[ 13579][26]| [2468] [048])  0209)) $/; //tests have been done

How to determine the date yyyyMMdd with regular expressions . The date judgment involves leap year common year judgment, Otsuki Yue's judgment and so on. Since a long time useless regular expression, so can only step-by-step to achieve the judgment of the date. Finally succeeded, share the process below.

First of all, I think of Otsuki's judgment. 1,3,5,7,8,10,12 These months are a few days, there are similarities, but slightly different.

(0[13578]|10|12)(0[1-9]|1[0-9]|2[0-9]|3[01])// The previous parenthesis is the judgment of the month, and the latter bracket is the judgment of the date

Test the success, next to the judgment of the moon, the month of the day has 4,6,9,11. Basically similar to judging Otsuki:

(0[469]|11) (0[1-9]|1[0-9]|2[0-9]|30)

Then Judge 2 months, first consider The circumstances of the day

02 (0[1-9]|1[0-9]|2[0-8])

Now that the month and date are judged, consider the year. The range of years here is 0001 to 9999. The regular expression of the judgment is:

[0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9]| [0-9] [1-9] [0-9] {2}| [1-9] [0-9] {3}< xmlnamespace prefix = "o" ns = "Urn:schemas-microsoft-com:office:office"/>

Then consider the case of leap year, leap year is divided into two cases, one is hundred years can be evenly divisible, the other is the whole hundred years can be divisible by 4 . The whole hundred years of the situation, mainly the top two:04,08; the first one is even,[2468][048]; the first one is odd,[13579][26]:

(0[48]|[ 13579][26]| [2468] [048]) XX

Another leap year situation, not a century can be divisible by 4 years, mainly considering the latter two potential is divisible by 4 , and similar to the above also divided the odd even:

[0-9] {2} (0[48]| [13579] [26]| [2468] [048])

combine a leap year:

((0[48]| [13579] [26]| [2468] [048]) 00) | ([0-9]{2} (0[48]|[ 13579][26]| [2468] [048]))

Now all the conditions have been considered, and the rest is the merger.

Regardless of the 2 -month number of leap years:

([0-9]{3}[1-9]|[ 0-9]{2}[1-9][0-9]| [0-9] [1-9] [0-9] {2}| [1-9] [0-9] {3} ) ((0[13578]|10|12)(0[1-9]|1[0-9]|2[0-9]|3[01]) | ( 0[469]|11) (0[1-9]|1[0-9]|2[0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8]))

The final expression for the 2 -month period considering a leap year is:

([0-9]{3}[1-9]|[ 0-9]{2}[1-9][0-9]| [0-9] [1-9] [0-9] {2}| [1-9] [0-9] {3} ) ((0[13578]|10|12)(0[1-9]|1[0-9]|2[0-9]|3[01]) | ( 0[469]|11) (0[1-9]|1[0-9]|2[0-9]|30) |02 (0[1-9]|1[0-9]|2[0-8])| (((0[48]|[ 13579][26]| [2468] [048]) 000209) | ([0-9]{2} (0[48]|[ 13579][26]| [2468] [048]) 0209

Date format Validation

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.