datetime object from string

Discussion in 'Python' started by Douglas Douglas, Feb 7, 2006.

  1. Hi everybody.

    I need to create a datetime object from a string like "20/01/2005 15:10:01". I
    know the mxDateTime module can do this with the DateTimeFrom method, but I was
    wondering if is possible to do this using only the standard library.

    I read the datetime object reference but didn't find a method that does it
    directly.

    Is it possible to do it with the standard library?

    Thanks all.

    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
    Douglas Douglas, Feb 7, 2006
    #1
    1. Advertising

  2. Douglas Douglas wrote:
    > Hi everybody.
    >
    > I need to create a datetime object from a string like "20/01/2005 15:10:01". I
    > know the mxDateTime module can do this with the DateTimeFrom method, but I was
    > wondering if is possible to do this using only the standard library.
    >
    > I read the datetime object reference but didn't find a method that does it
    > directly.
    >
    > Is it possible to do it with the standard library?


    Here's one way:

    >>> import time, datetime
    >>> s = '20/01/2005 15:10:01'
    >>> time_tuple = time.strptime(s, "%d/%m/%Y %H:%M:%S")
    >>> datetime.datetime(*time_tuple[:6])

    datetime.datetime(2005, 1, 20, 15, 10, 1)

    http://docs.python.org/lib/datetime-datetime.html
    http://docs.python.org/lib/module-time.html

    Raymond
    Raymond Hettinger, Feb 7, 2006
    #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. Christos TZOTZIOY Georgiou
    Replies:
    3
    Views:
    702
    Christos TZOTZIOY Georgiou
    Sep 13, 2003
  2. Tim Peters
    Replies:
    0
    Views:
    529
    Tim Peters
    Sep 9, 2003
  3. mp
    Replies:
    1
    Views:
    385
    John Machin
    Jul 28, 2006
  4. Martin
    Replies:
    0
    Views:
    333
    Martin
    Dec 27, 2008
  5. Replies:
    2
    Views:
    752
    M.-A. Lemburg
    Jan 6, 2009
Loading...

Share This Page