Parsing text into dates?

John Roth newsgroups at jhrothjr.com
Mon May 16 22:16:25 EDT 2005


"Thomas W" <thomas.weholt at gmail.com> wrote in message 
news:1116277171.492587.38080 at g47g2000cwa.googlegroups.com...
> I'm developing a web-application where the user sometimes has to enter
> dates in plain text, allthough a format may be provided to give clues.
> On the server side this piece of text has to be parsed into a datetime
> python-object. Does anybody have any pointers on this?
>
> Besides the actual parsing, my main concern is the different locale
> date formats and how to be able to parse those strange us-like
> "month/day/year" compared to the clever and intuitive european-style
> "day/month/year" etc.
>
> I've searched google, but haven't found any good referances that helped
> me solve this problem, especially with regards to the locale date
> format issues.

There is no easy answer if you want to be able to enter three
numbers. There are two answers that work, although there will
be a lot of complaining. One is to use the international yyyy-mm-dd
form, and the other is to accept a 4 digit year, an alphabetic month
and a two digit day in any order.

Otherwise, if you get 4 digits as the first component, and it passes your
validation (whatever that is) for reasonable years, you're probably
pretty safe to assume that you've got yyyy-mm-dd. Otherwise
if you can't get a clean answser (one is > 31, one is 12 < x < 32
and one is <= 12, just give them a list of possibilities and politely
suggest that they enter it as yyyy-mm-dd next time.

I don't validate separators. As long as there is something that isn't a
number or a letter, it's a separator and which one doesn't matter. At
times I've even taken the transition between a digit and a letter as
a separator.

John  Roth

> Best regards,
> Thomas
> 




More information about the Python-list mailing list