Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex date validation for yyyy-mm-dd [duplicate]

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.

like image 663
Romaine Herrera Avatar asked Feb 27 '14 07:02

Romaine Herrera


People also ask

Which of the following is the correct regex to match the date format dd mm yyyy?

To match a date in mm/dd/yyyy format, rearrange the regular expression to ^(0[1-9]|1[012])[- /.] (0[1-9]|[12][0-9]|3[01])[- /.] (19|20)\d\d$. For dd-mm-yyyy format, use ^(0[1-9]|[12][0-9]|3[01])[- /.]

How do you validate a date in YYYY MM DD format in Java?

DateValidator validator = new DateValidatorUsingDateFormat("MM/dd/yyyy"); assertTrue(validator. isValid("02/28/2019")); assertFalse(validator. isValid("02/30/2019")); This was the most common solution before Java 8.

What is regex in validation?

The Validation (Regex) property helps you define a set of validation options for a given field. In general, this field property is used to perform validation checks (format, length, etc.) on the value that the user enters in a field. If the user enters a value that does not pass these checks, it will throw an error.

How to validate DD/MM/YYYY date format using regex in Java?

How to validate dd/mm/yyyy date format using regex in Java? The structure of the date in the format “dd/mm/yyyy” needs to have 2 digits as day followed by /, followed by 2 digits for the month followed by /, and then 4 digits for the year. Let’s try to come up with a simple regex to validate this.

What is the YYYY-MM-DD date format?

This regex code will accept the YYYY-MM-DD date format with some degree of intelligence. ISO 8601 allows for date representations without the hyphens separating the parts of the date. For example 14 June 2021 can be written as 2021-06-14 in the extended human-readable format but also as 20210614 in the basic format.

What are the date formats supported by regex?

Regex to validate date formats dd/mm/YYYY, dd-mm-YYYY, dd.mm.YYYY, dd mmm YYYY, dd-mmm-YYYY, dd/mmm/YYYY, dd.mmm.YYYY with Leap Year Support Ask Question Asked8 years, 9 months ago Active2 months ago Viewed807k times 223

How do you write a regular expression for a date?

A regular expression for dates (YYYY-MM-DD) should check for 4 digits at the front of the expression, followed by a hyphen, then a two-digit month between 01 and 12, followed by another hyphen, and finally a two-digit day between 01 and 31. Here’s the regex code that does all this: /^d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/


3 Answers

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

like image 171
Vinod Avatar answered Sep 20 '22 18:09

Vinod


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.

like image 33
wumpz Avatar answered Sep 20 '22 18:09

wumpz


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.

like image 10
Hamid Shatu Avatar answered Sep 21 '22 18:09

Hamid Shatu