convert date time

Kushal Kumaran kushal.kumaran+python at gmail.com
Sat Aug 22 09:11:49 CEST 2009


On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross<ronn.ross 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)
>
> I want to split it into two fields one with the date formatted like this:
> YYYY-MM-DD  2009-08-02
>
> and the time to be 24 hour or military time. How every you call it. Similar
> to this:
> 15:22:00
>
> I found it easy to truncate off the (UTC), but having trouble converting the
> date. Can someone point me in the right direction?
>

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.

-- 
kushal



More information about the Python-list mailing list