convert date time

John Machin sjmachin at lexicon.net
Sat Aug 22 05:29:53 EDT 2009


On Aug 22, 5:11 pm, Kushal Kumaran <kushal.kumaran+pyt... at gmail.com>
wrote:
> On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross<ronn.r... at gmail.com> wrote:
> > I'm new to python and I'm getting a date time from a field in the database
> > that looks like this:
> > 8/2/2009 8:36:16 AM (UTC)

> datetime.datetime.strptime() will give you a datetime object, which
> you can then format in a wide variety of ways using its strftime()
> method.  Doesn't work with dates earlier than 1900, I believe.

The datetime module works from 0001-01-01 onwards; see
http://docs.python.org/library/datetime.html#datetime.MINYEAR

Other things worth mentioning:

(1) datetime.datetime.strptime() was introduced in Python 2.5; for
Python 2.3 and 2.4, use time.strptime() [which may not like years
before 1970] followed by datetime.datetime()

(2) as the input has 12-hour clock plus AM/PM, ensure that you use %I
instead of %H for the hour component; %H won't do what you expect if
your expectation is not based on reading the docs :-)



More information about the Python-list mailing list