Alex Rivera | Logout

How can I account for period (AM/PM) using strftime?

Asked 2009-11-18T22:04:41.920
228

Specifically I have code that simplifies to this:

from datetime import datetime
date_string = '2009-11-29 03:17 PM'
format = '%Y-%m-%d %H:%M %p'
my_date = datetime.strptime(date_string, format)

# This prints '2009-11-29 03:17 AM'
print my_date.strftime(format)

What gives? Does Python just ignore the period specifier when parsing dates or am I doing something stupid?

Edit
Report

1 Answer

38

You used %H (24 hour format) instead of %I (12 hour format).

answered 2009-11-18T22:09:19.020

Your Answer