user1028861
user1028861

Reputation:

Python datetime: 21-Feb-12 to %d-%m-%Y

Is there an easy (built-in) way to convert 21-Feb-12 to %d-%m-%Y using datetime and/or calendar? Or will I need to parse the string "21-Feb-12"? Any Pythonic suggestions welcome. Thanks.

Upvotes: 2

Views: 2617

Answers (1)

jdi
jdi

Reputation: 92569

Use strptime and give it the proper format.

from datetime import datetime
print datetime.strptime("21-Feb-12", "%d-%b-%y")
#datetime.datetime(2012, 2, 21, 0, 0)

Here is the strptime reference: http://docs.python.org/library/datetime.html#strftime-strptime-behavior

Upvotes: 2

Related Questions