datetime object from string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Douglas Douglas

    #1

    datetime object from string

    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
    Yahoo Mail: Your smarter, faster, free email solution. Organize your inbox, protect your privacy, and tackle tasks efficiently with AI-powered features and robust security tools.

  • Raymond Hettinger

    #2
    Re: datetime object from string

    Douglas Douglas wrote:[color=blue]
    > 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?[/color]

    Here's one way:
    [color=blue][color=green][color=darkred]
    >>> import time, datetime
    >>> s = '20/01/2005 15:10:01'
    >>> time_tuple = time.strptime(s , "%d/%m/%Y %H:%M:%S")
    >>> datetime.dateti me(*time_tuple[:6])[/color][/color][/color]
    datetime.dateti me(2005, 1, 20, 15, 10, 1)


    This module provides various time-related functions. For related functionality, see also the datetime and calendar modules. Although this module is always available, not all functions are available...


    Raymond

    Comment

    Working...