datetime object from string

  • Thread starter Douglas Douglas
  • Start date
D

Douglas Douglas

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
 
R

Raymond Hettinger

Douglas said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top