Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11353

Re: How do I convert String into Date object

Date 2011-08-13 22:11 +0200
From Rafael Durán Castañeda <rafadurancastaneda@gmail.com>
Subject Re: How do I convert String into Date object
References <83822ecb-3643-42c6-a2bf-0187c07d351d@a10g2000yqn.googlegroups.com> <6f4283c7-794c-41f1-93fd-6d9be73894c2@w11g2000vbp.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2255.1313266271.1164.python-list@python.org> (permalink)

Show all headers | View raw


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...@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,

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 12:14 -0700
  Re: How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 12:26 -0700
    Re: How do I convert String into Date object Rafael Durán Castañeda <rafadurancastaneda@gmail.com> - 2011-08-13 22:11 +0200
      Re: How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 13:25 -0700
    Re: How do I convert String into Date object Peter Otten <__peter__@web.de> - 2011-08-13 22:29 +0200
    Re: How do I convert String into Date object Sibylle Koczian <nulla.epistola@web.de> - 2011-08-13 22:05 +0200
  Re: How do I convert String into Date object Chris Rebert <clp2@rebertia.com> - 2011-08-13 13:09 -0700
  Re: How do I convert String into Date object Roy Smith <roy@panix.com> - 2011-08-13 20:49 -0400
  Re: How do I convert String into Date object MrPink <tdsimpson@gmail.com> - 2011-08-13 22:44 -0700

csiph-web