How do I convert String into Date object

Sibylle Koczian nulla.epistola at web.de
Sat Aug 13 22:05:33 CEST 2011


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:

 >>> import datetime
 >>> oDateTime = datetime.datetime.strptime('07/27/2011', '%m/%d/%Y')
 >>> oDateTime
datetime.datetime(2011, 7, 27, 0, 0)
 >>> oDate = oDateTime.date()
 >>> oDate
datetime.date(2011, 7, 27)
 >>>

HTH
Sibylle




More information about the Python-list mailing list