[Tutor] Formating from hhmms to hh:mm:ss

Kent Johnson kent37 at tds.net
Sun Sep 7 20:54:22 CEST 2008


On Sun, Sep 7, 2008 at 1:17 PM, greg whittier <greg at thewhittiers.com> wrote:

>> # program to test str... functions
>> import datetime
>
> Are you sure you didn't do "from datetime import datetime"?

or "import time" ?

>> ...
>> # format conversion of date+time
>> dt1 = datetime.strptime("20080421_101145", "%Y%m%d_%H%M%S")
>
> I don't see how this works as written because the datetime module doesn't
> have a strptime function.
>>
>> print "dt1: ",dt1
>> other = dt1.strftime("%Y%m%d_%H%M%S")  <-- fails, line 11
>> print other.replace(" ", "")
>>
>> Results:
>> dt1:  (2008, 4, 21, 10, 11, 45, 0, 112, -1)

Looks to me like you are confusing the time and datetime modules.
time.strptime() will return a time struct;
datetime.datetime.strptime() returns a datetime.datetime.

Kent


More information about the Tutor mailing list