[Tutor] Formating from hhmms to hh:mm:ss
greg whittier
greg at thewhittiers.com
Sun Sep 7 22:24:59 CEST 2008
On Sun, Sep 7, 2008 at 1:36 PM, Wayne Watson
<sierra_mtnview at sbcglobal.net>wrote:
> Yes, cut and paste directly from the code. Positively a import as seen.
> Here's the full set of code:
>
> # The effect of adding seconds to date-time to see if day gets changed
> import datetime
> dt1 = datetime.datetime(2008, 03, 10, 23, 59, 0)
> print dt1
> delta = datetime.timedelta(seconds = 200)
> print dt1+delta
>
> # format conversion of date+time
> dt1 = time.strptime("20080421_101145", "%Y%m%d_%H%M%S")
>
> The first code you posted was different. You had datetime.strptime and not
time.strptime. Your problem is dt1 is a time.struct_time which has no
strftime method, producing the error.
You must have imported time somewhere. This line makes dt1 a
time.struct_time which causes your problem.
>
> print "dt1: ",dt1
> other = dt1.strftime("%Y%m%d_%H%M%S")
>
> Now dt1 is a time.struct_time and not a datetime.datetime object so it has
no strftime method.
>
> ...
>> # 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.
>
>>
This is what you had before. Note datetime.strptime instead of
time.strptime, which confused me because the datetime module doesn't have a
strptime function.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080907/28950532/attachment-0001.htm>
More information about the Tutor
mailing list