87

what is the regex for the date format yyyy-mm-dd?

I want to validate the email from edittext and check if it matches the regex.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
Romaine Herrera
  • 879
  • 1
  • 7
  • 5
  • Also have a look to the right at the "Related" section, there are many similar cases of requests for RegEx strings for date validation. – CJBS Feb 27 '14 at 07:25
  • Check this answer it may help http://stackoverflow.com/a/2149698/2675669 – Nambi Feb 27 '14 at 07:34

4 Answers4

202

This will match yyyy-mm-dd and also yyyy-m-d:

^\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])$

Regular expression visualization

If you're looking for an exact match for yyyy-mm-dd then try this

^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$

or use this one if you need to find a date inside a string like The date is 2017-11-30

\d{4}\-(0?[1-9]|1[012])\-(0?[1-9]|[12][0-9]|3[01])*

https://regex101.com/r/acvpss/1

rboy
  • 2,018
  • 1
  • 23
  • 35
Vinod
  • 4,672
  • 4
  • 19
  • 26
76

A simple one would be

\d{4}-\d{2}-\d{2}

Regular expression visualization

Debuggex Demo

but this does not restrict month to 1-12 and days from 1 to 31.

There are more complex checks like in the other answers, by the way pretty clever ones. Nevertheless you have to check for a valid date, because there are no checks for if a month has 28, 30, or 31 days.

wumpz
  • 8,257
  • 3
  • 30
  • 25
  • 3
    Wow. A first downvote after nearly 4 years. Isn't this worth another badge? ;) – wumpz Feb 07 '18 at 07:35
  • 2
    Why plain wrong? The regex and stated limitations are correct. What do I miss? – wumpz Jan 05 '19 at 14:54
  • 1
    It's not a real answer to the question because it does not validate dates, just validates "4 digits followed by a dash followed by 2 digits followed by a dash followed by 2 digits". Just like saying, 2+2 is 3 but you need to add 1. It's missleading. – Sebastian Jan 06 '19 at 18:40
  • 2
    It is also missleading to add this comment only to this answer. The other answers are also incorrect in this sense because they are also incomplete to validate it. But if you prefer I would call it a remark or large comment. – wumpz Jan 06 '19 at 19:31
  • It's the fact that it has too many votes, nothing more in particular. – Sebastian Jan 09 '19 at 03:48
  • 13
    Upvoting to compensate for the downvote because it's the only answer that actually acknowledges its limitations. The higher voted answers don't invalidate Feb 31, for example. Capture the text fields with regex and validate with other sanity checks. – Daniel Widdis May 14 '20 at 23:39
12

You can use this regex to get the yyyy-MM-dd format:

((?:19|20)\\d\\d)-(0?[1-9]|1[012])-([12][0-9]|3[01]|0?[1-9])

You can find example for date validation: How to validate date with regular expression.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
7

you can test this expression:

^\d{4}[\-\/\s]?((((0[13578])|(1[02]))[\-\/\s]?(([0-2][0-9])|(3[01])))|(((0[469])|(11))[\-\/\s]?(([0-2][0-9])|(30)))|(02[\-\/\s]?[0-2][0-9]))$

Description:
validates a yyyy-mm-dd, yyyy mm dd, or yyyy/mm/dd date

makes sure day is within valid range for the month - does NOT validate Feb. 29 on a leap year, only that Feb. Can have 29 days

Matches (tested) : 0001-12-31 | 9999 09 30 | 2002/03/03

zainoz.zaini
  • 918
  • 6
  • 20