Re: convert date time

Discussion in 'Python' started by Kushal Kumaran, Aug 22, 2009.

  1. On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross<> 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
     
    Kushal Kumaran, Aug 22, 2009
    #1
    1. Advertising

  2. Kushal Kumaran

    John Machin Guest

    On Aug 22, 5:11 pm, Kushal Kumaran <>
    wrote:
    > On Fri, Aug 21, 2009 at 11:44 PM, Ronn Ross<> 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 :)
     
    John Machin, Aug 22, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Berg
    Replies:
    0
    Views:
    797
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
  3. wgan
    Replies:
    7
    Views:
    599
    Roedy Green
    Jul 8, 2004
  4. Keith Cochrane
    Replies:
    2
    Views:
    626
    Keith Cochrane
    Aug 6, 2006
  5. Replies:
    8
    Views:
    787
    Boris
    May 1, 2007
Loading...

Share This Page