How do I convert String into Date object

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Sat Aug 13 22:11:00 CEST 2011


You can use datetime objects:

 >>> dt1 = datetime.datetime.strptime('07/27/2011',"%m/%d/%Y")
 >>> dt2 =datetime.datetime.strptime('07/28/2011',"%m/%d/%Y")
 >>> dt1 == dt2
False
 >>> dt1 > dt2
False
 >>> dt1 < dt2
True
 >>> dt1 - dt2
datetime.timedelta(-1)


On 13/08/11 21:26, MrPink wrote:
> 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,
>
> On Aug 13, 3:14 pm, MrPink<tdsimp... at gmail.com>  wrote:
>> 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,




More information about the Python-list mailing list