Converting strings to dates

  • Thread starter Chermside, Michael
  • Start date
C

Chermside, Michael

I'm trying to convert a string back into a datetime.date.

First I'll create the string:


Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.2005-02-04

Now I convert it back:
datetime.date(2005, 2, 4)


WOW, that's ugly. Is there a more concise way to do this?

-- Michael Chermside


This email may contain confidential or privileged information. If you believe you have received the message in error, please notify the sender and delete the message without copying or disclosing it.
 
K

Kartic

py> import time
py> date_str = time.strftime('%Y-%m-%d', time.localtime())
py> date_str
'2005-02-04'
py> time.strptime(date_str, '%Y-%m-%d')
(2005, 2, 4, 0, 0, 0, 4, 35, -1)

That work?
 
D

Dan Bishop

I'm trying to convert a string back into a datetime.date.

First I'll create the string:


Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.2005-02-04

Now I convert it back:
datetime.date(2005, 2, 4)


WOW, that's ugly. Is there a more concise way to do this?

datetime.date(*map(int, s.split("-")))
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top