0

I have the regular expression as:

var validDate = /^\d{4}-((0\d)|(1[012]))-(([012]\d)|3[01]) (([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9]):([0-5]?[0-9]).([0-9]?[0-9]?[0-9])$/i.test(value);

The value I am providing is Nov 21, 2014 12:00:00 AM.

Can anybody please help me in providing with the actual date expression (value) that will actually test the regex?

3
  • try this one 2014-02-31 23:00:59999 that will pass the above test, exposing two bugs, one easily fixed, the other not so easy. Commented Dec 24, 2014 at 7:15
  • the above test requires a digit one place after the seconds part of the time. the expression looks like a failed attempt to describe some variant of ISO-9601. 2014-02-31 23:00:666 also "works" Commented Dec 24, 2014 at 7:19
  • Do not parse dates with RegExp. Use a date parser. Commented Dec 24, 2014 at 11:08

3 Answers 3

1

Why not use built-in Date.parse:

var epochVal = Date.parse("Nov 21, 2014 12:00:00 AM")
//=> 1416546000000

OR:

var dt = new Date( Date.parse("Nov 21, 2014 12:00:00 AM") )
//=> Fri Nov 21 2014 00:00:00 GMT-0500 (EST)
Sign up to request clarification or add additional context in comments.

Comments

0

Can anybody please help me in providing with the actual date expression (value) that will actually test the regex.

I won't provide the value, but rather the format of what the regex matches:

####-mm-dd HH:MM:ss.###

Where # denotes a number, mm denotes a month, dd denotes a date, HH denotes an hour in 24-hour format, MM denotes minutes and ss denotes seconds.

3 Comments

and (bug) the . could be any character at all!
@Jasen Oh yea, nicely spotted.
Also, the regex can accept any date, even if it is not valid like the 30th of February.
0

try this one 2014-02-13 23:00:59.0

2 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
@remus It answers this bit. Can anybody please help me in providing with the actual date expression (value) that will actually test the regex., well "yes" would be technically more accurate but it too short to satisfy stack exchange.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.