[Tutor] Time module

Kent Johnson kent37 at tds.net
Tue Dec 4 23:21:51 CET 2007


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


More information about the Tutor mailing list