-2

I need a regular expression to match

yyyy/MM/dd HH:mm

and another one match

dd/MM/yyyy HH:mm

and also check to for 30 and 31 and. using JavaScript

Match for example

2000/02/28 11:55

28/02/2000 11:55

not match for example

2000/02/31 11:55

31/02/2000 11:55

1 Answers1

0

I'll help with the regex, if that's something you want to understand:

If you want to match yyyy/MM/dd HH:mm then use

\d{4}\/\d{2}\/\d{2}\s\d{2}:\d{2}

See it working here

To explain:

\d{4} four digits (year) yy
\/ escape slash
\d{2} two digits (month) MM
\/ escape slash
\d{2} two digits (date) dd
\s single space character
\d{2} two digits (hour) hh
: colon (string literal)
\d{2} two digits (minutes) mm

For your other version just swap over the 4 and 2

\d{2}\/\d{2}\/\d{4}\s\d{2}:\d{2}

You can then check the date for valid numbers - but as Tim Biegeleisen says, you should check for all months.

Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • 1
    i want to not match 2000/02/31 11:55 – Shereen Bashar Aug 26 '17 at 11:27
  • That's not gonna happen in a regular expression. You first need to validate a string is in time format you want and **then** you can determine if it's a valid date or not. Have a look at [this post](https://stackoverflow.com/questions/21188420/javascript-date-validation-not-validation-february-31), it may help you – Ghoul Fool Aug 26 '17 at 12:32