Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression for two-digit month and two-digit day only

Tags:

date

regex

I've been searching high and low for a regex that will validate MM/DD only. I do not need year (we're asking in a form for fiscal year ends, so we don't need a year, we need to know the range of their fiscal year).

For example, I need them to be able to enter 06/30 or 12/31 for Jun 30th or Dec 31st, respectively. All the regex examples I was able to find include the year as part of the date, which I do not need and I am very unfamiliar with regular expression syntax. What regex can I use for for two digit month and day only?

like image 387
shayaknyc Avatar asked Dec 04 '22 01:12

shayaknyc


1 Answers

For all months (i.e. 28 days):

(0[1-9]|1[0-2])/([01][1-9]|10|2[0-8])

For all months apart from February:

(0[13-9]|1[0-2])/(29|30)

For months with 31 days (i.e.01, 03, 05,07, 08, 10 and 12)

(0[13578]|1[0-2])/31

Putting this together

((0[1-9]|1[0-2])/([01][1-9]|10|2[0-8]))|((0[13-9]|1[0-2])/(29|30))|((0[13578]|1[0-2])/31)

Then for leap years append the following to the regular expression

|02/29
like image 152
Ed Heal Avatar answered Jan 18 '23 11:01

Ed Heal