The Joel on Software Discussion Group (CLOSED)

A place to discuss Joel on Software. Now closed.

This community works best when people use their real names. Please register for a free account.

Other Groups:
Joel on Software
Business of Software
Design of Software (CLOSED)
.NET Questions (CLOSED)
Fog Creek Copilot

The Old Forum

Your hosts:
Albert D. Kallal
Li-Fan Chen
Stephen Jones

Ultimate regex date validation?

I've googled and googled, but still have not found the ultimate regex date validation.

I need something that will first try to correct the nonsense a user enters f.x. 1. May 2004 or 10/10/2005 or 1.2.2004 (attempt to handle US and EU formats), and then hand over the 'correct' string to the second phase, that does the actual validation.
Monday, October 17, 2005
Just parse the damn date - why "validate it" first?

If the parse works, you have the date.

If you have the date, you can assume that the input is correct.

Otherwise it is obviously invalid.
Arafangion Send private email
Monday, October 17, 2005
PHP has a strtotime() functions that does that. It's much more than a regex - see

I guess other platforms have something similar.
Sagi B Send private email
Monday, October 17, 2005
Yes, all that function does is return a date given an input string.

It doesn't first say "Hmm, does this look like it might be a date?"

No, it just gets a date.  It probably returns some null value if it can't, but at no point does it just do:

1) Get the input string
2) Check to see if it "looks" like a date.
3) Check to see if it "looks" like it can't possibly be a date.
4) Go and hand over the input string to the "real" date function, which actually gets the date from the string.
Arafangion Send private email
Monday, October 17, 2005
Recently I've been attempting to convert a particular database.

I would just like to mention that you should NOT allow both EU and US dates.

It's really awful when any style is allowed (For instance, Access 97 does this, afaik).

What date is the following: 99/10/10

9 oct 0010?
9 oct 1910?
9 oct 2010?
(And again for all the other permutations)

Typically what I find happens is that dates default to US, unless it can only possibly be EU, or something other, which is a royal pain in the **** for us EU users!

So, moral of the story is pick only one format, and use that.

For system tasks I like year-month-day, and for user tasks I like something localized and abstracted, yet configuable via system defaults.
Arafangion Send private email
Monday, October 17, 2005
Your database was storing dates as text fields, rather than using the native date type?

If you need to specify a date format, look at ISO 8601. In a nutshell it's YYYY-MM-DD, and if you add time it includes functionality for timezones.
Dennis Forbes Send private email
Monday, October 17, 2005
randall schwartz (of perl fame) says one cannot use a regex to validate/parse all date formats.
YaYaYa Send private email
Monday, October 17, 2005
If you have GNU date installed, just run in a subshell:

date -d '10:00 jan 1 2003' +%s

Or go read the source to GNU date.
Mike Schiraldi
Monday, October 17, 2005
>It doesn't first say "Hmm, does this look like it might be a date?"

And what's your problem with that? If it returns a date, then the thing obviously looked like a date. If it doesn't, then thing thing obviously didn't look like a date. Are you worried about the extra CPU cycles or something?

No regex can completely check a date for validity. Regexes can only do a certain limited form of pattern patching (read up on regular grammars), they aren't a magical cut-and-paste answer to all data validation problems
Matt Send private email
Monday, October 17, 2005
Matt, I was disputing the sense of first looking to see if the date was valid, then parsing it.

In other words, the useage of some regexp to check to see if it "looks" ok, then the useage of some real parser to get the real date.

email addresses are another common victim of regexp matches.
Arafangion Send private email
Monday, October 17, 2005
I found this one (date only, and only accurate for 1/1/1600 and later) after a short amount of Googling:


If you need time as well, this one (by the same author, and valid for 01/01/0001 or later) will handle that also:

Thursday, October 20, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz