0

I have tried

^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|12[0-9]|3[01]T(0[0-9]|1[0-9]|2[0123]):(0[0-9]|12345[0-9]):(0[0-9]|12345[0-9]))$

and

^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|12[0-9]|3[01][T](0[0-9]|1[0-9]|2[0123])[:](0[0-9]|12345[0-9])[:](0[0-9]|12345[0-9]))$

and neither of them worked on a sample datetime - 2009-06-15T13:45:30

miken32
  • 42,008
  • 16
  • 111
  • 154
Coding Duchess
  • 6,445
  • 20
  • 113
  • 209

4 Answers4

3

You have missed a parenthesis and add an extra one at the end:

^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[[12][0-9]|3[01])T(0[0-9]|1[0-9]|2[0123]??):(0[0-9]|[12345][0-9]):(0[0-9]|[12345][0-9])$ 
//                          add parenthesis here       __^                                           and delete the one here __^

You may also simplify to:

^(19|20)\d\d-(0[1-9]|1[012])-([012]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$ 

Another way to go to test only the format then test the validity is:

^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)$

and then test the validity:

  • group1 between 1900 and 2000
  • group2 between 01 and 12
  • group3 between 01 and 31
  • group4 between 00 and 23
  • group5 between 00 and 59
  • group6 between 00 and 59

Or, better, use a date parser;, I'm there is one that exists in your favorite language.

Vitaly
  • 3
  • 2
Toto
  • 89,455
  • 62
  • 89
  • 125
1

Toto's answer fixes all your problems but doesn't tell you why.

You have 4 mistakes:

  1. wrongly placed parenthesis

    (0[1-9]|12[0-9]|3[01]T -> (0[1-9]|12[0-9]|3[01])T

  2. lack of brackets on hours first digit

    (0[1-9]|12[0-9]|3[01]) -> (0[1-9]|[12][0-9]|3[01])

  3. lack of brackets on seconds first digit

    (0[0-9]|12345[0-9]) -> (0[0-9]|[12345][0-9])

  4. Missplaced parenthesis at the end

    (0[0-9]|12345[0-9]))$ -> (0[0-9]|12345[0-9])$

For the optimization of this, I will quote Toto's answer.

^(19|20)\d\d-(0[1-9]|1[012])-([012]\d|3[01])T([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$

this is your second solution before the fixes:

^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|12[0-9]|3[01][T](0[0-9]|1[0-9]|2[0123])[:](0[0-9]|12345[0-9])[:](0[0-9]|12345[0-9]))$

enter image description here

this is your second solution after the fixes:

^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[1]2[0-9]|3[01])[T](0[0-9]|1[0-9]|2[0123])[:](0[0-9]|12345[0-9])[:](0[0-9]|[12345][0-9])$

enter image description here

Taochok
  • 359
  • 4
  • 7
1

This is my working recipe

const exp  = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/g;
Sameh
  • 1,318
  • 11
  • 11
0

You have for the day part 12[0-9] which is the problem. Probably mean [12][0-9]

Sami Kuhmonen
  • 30,146
  • 9
  • 61
  • 74
  • Sami, I have the same issue as with date with minutes and seconds so I changed my code to ^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[[12][0-9]|3[01]T(0[0-9]|1[0-9]|2[0123]):(0[0-9]|[12345][0-9]):(0[0-9]|[12345][0-9]))$ but still no match found – Coding Duchess Apr 23 '15 at 14:29