How do I convert String into Date object

M

MrPink

Is this the correct way to convert a String into a Date?
I only have dates and no time.

import time, datetime

oDate = time.strptime('07/27/2011', '%m/%d/%Y')
print oDate

Thanks,
 
M

MrPink

I have file of records delimited by spaces.
I need to import the date string and convert them into date datatypes.

'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'

I just discovered that my oDate is not an object, but a structure and
not a date datatype.
I'm stumped. Is there a way to convert a string into a date datatype
for comparisons, equality, etc?

Thanks,
 
S

Sibylle Koczian

Am 13.08.2011 21:26, schrieb MrPink:
I have file of records delimited by spaces.
I need to import the date string and convert them into date datatypes.

'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'

I just discovered that my oDate is not an object, but a structure and
not a date datatype.
I'm stumped. Is there a way to convert a string into a date datatype
for comparisons, equality, etc?
There is. Only not in one step, if you want a date and not a datetime
instance:

HTH
Sibylle
 
C

Chris Rebert

Is this the correct way to convert a String into a Date?
I only have dates and no time.

import time, datetime

oDate = time.strptime('07/27/2011', '%m/%d/%Y')
print oDate

from datetime import datetime
the_date = datetime.strptime('07/27/2011', '%m/%d/%Y').date()

Cheers,
Chris
 
M

MrPink

BTW, here is the Python version I'm using.
Will this make a difference with the solutions you guys provided?

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin

Also, what editor do you guys use?
There are so many to chose from.
 
P

Peter Otten

MrPink said:
I have file of records delimited by spaces.
I need to import the date string and convert them into date datatypes.

'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'

I just discovered that my oDate is not an object, but a structure and
not a date datatype.
I'm stumped. Is there a way to convert a string into a date datatype
for comparisons, equality, etc?

Thanks,

That looks like a fifty-percent chance to try the "wrong" module ;)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: type object 'datetime.date' has no attribute 'strptime'

So you cannot construct a date from a date string either.
One more time:
datetime.date(2011, 7, 27)

$ cat csv_dates.csv
'07/27/2011' 'Event 1 Description'
'07/28/2011' 'Event 2 Description'
'07/29/2011' 'Event 3 Description'
$ cat csv_dates.py
import datetime
import csv

def rows(instream):
for row in csv.reader(instream, delimiter=" ", quotechar="'"):
row[0] = datetime.datetime.strptime(row[0], '%m/%d/%Y').date()
yield row

if __name__ == "__main__":
import sys
filename = sys.argv[1]

with open(filename, "rb") as instream:
for row in rows(instream):
print row
$ python csv_dates.py csv_dates.csv
[datetime.date(2011, 7, 27), 'Event 1 Description']
[datetime.date(2011, 7, 28), 'Event 2 Description']
[datetime.date(2011, 7, 29), 'Event 3 Description']
 
R

Roy Smith

MrPink said:
Is this the correct way to convert a String into a Date?
I only have dates and no time.

You have already received a number of good replies, but let me throw out
one more idea. If you ever need to do something with dates and times
which the standard datetime module can't handle, take a look at the
excellent dateutil module by Gustavo Niemeyer
(http://labix.org/python-dateutil).
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,811
Latest member
SaulFernan

Latest Threads

Top