Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > datetime object from string

Reply
Thread Tools

datetime object from string

 
 
Douglas Douglas
Guest
Posts: n/a
 
      02-07-2006
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
 
Reply With Quote
 
 
 
 
Raymond Hettinger
Guest
Posts: n/a
 
      02-07-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: [2.4.4] creating a datetime.datetime from an XML xs:dateTime skip@pobox.com Python 2 01-06-2009 01:31 PM
[2.4.4] creating a datetime.datetime from an XML xs:dateTime Martin Python 0 12-27-2008 08:08 PM
mx.DateTime to datetime.datetime mp Python 1 07-28-2006 10:57 PM
datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Christos TZOTZIOY Georgiou Python 3 09-13-2003 10:44 AM
RE: datetime: .datetime-.datetime = .timedelta, .time-.time=TypeError ? Tim Peters Python 0 09-09-2003 12:57 AM



Advertisments