views:

240

answers:

5

I'm using python, and want to parsing a string like "01-Jan-1995" to datetime type of python. who can tell me how to do it? thanks.

+2  A: 

The standard date() object in python might be able to parse this sort of date. the dateutil should be even better at parsing this sort of format since you can define custom date formats to parse.

easy_install python-dateutil

to install it.

Soviut
+1 for python-dateutil. It can even parse datestrings without you having to tell it the format.
unutbu
+7  A: 

On the whole you'd parse date and time strings with the strptime functions in time or datetime modules. Your example could be parsed with:

import datetime
datetime.datetime.strptime("01-Jan-1995", "%d-%b-%Y")

Note that parsing month names is locale-dependent.

Tuure Laurinolli
A: 

If you need to parse natural language date and time strings, consider parsedatetime (and this answer).

Adam Matan