[Tutor] Time module
Tim Golden
mail at timgolden.me.uk
Wed Dec 5 09:52:27 CET 2007
Kent Johnson wrote:
> Norman Khine wrote:
>> Hello,
>> I am having difficulties in converting the following to display the
>> difference that has passed in hours and seconds in a nice way.
>>
>> from datetime import datetime
>> now = datetime.now()
>> posted = date
>> difference = now - posted
>>
>> namespace['date'] = date
>> namespace['posted'] = difference
>
> It would help if you would show complete code - what is date? what is
> namespace? Anyway, guessing that posted is a datetime.datetime, then
> difference is a datetime.timedelta.
>
>> when I look at the 'posted' namespace I get
>>
>> 23:08:31.454767
>>
>> What is the best way to split this so that it will show
>>
>> '23h 08m 31s' ago
>
> You will have to do some work yourself. difference has attributes days,
> seconds and microseconds. You can split the seconds into hours, minutes
> and seconds using % or divmod().
>
> Kent
Not a 100% match, but to show the kind of thing... have a look at this
code fragment (which I posted a while back to c.l.py).
http://groups.google.com/group/comp.lang.python/msg/40a87d00875f2958
TJG
More information about the Tutor
mailing list