0

What is the regular expression for date format (mm-dd-yyyy) and validating them?

Atish Kumar Dipongkor
  • 10,220
  • 9
  • 49
  • 77
Dinesh Dinaz
  • 313
  • 1
  • 4
  • 13

1 Answers1

2

Here it is

((0[1-9])|(1[0-2]))-(([0-2]\d)|([3][01]))-(\d{4})

Explanation:

  1. Since mm can be any thing from 01 to 12 so to suffice 01-09, regular expression would be 0[1-9] or as mm can also be 10-12 so 1[0-2].

    Hence the regular expression to match mm would be (0\d)|(1[0-2])

  2. As dd can be any thing from 01 to 31 so to suffice 01-29 regular expression would be [0-2]\d or it can also be 30-31 so [3][01].

    Hence the regular expression to match dd would be (([0-2]\d)|([3][01]))

  3. As yyyy can be any number of 4 digits i.e. 2010, 1999, 2050 etc (as long as you are not considering any specificrange) so the regular expression would simply be 4 digits i.e. \d{4}.

    Hence the regular expression to match yyyy would be (\d{4})

So after combining these, the complete regular expression for the date would be:

((0[1-9])|(1[0-2]))-(([0-2]\d)|([3][01]))-(\d{4})
Kamran Ahmed
  • 11,809
  • 23
  • 69
  • 101