Fractional Hours from datetime?

W. eWatson wolftracks at invalid.com
Mon Jan 11 19:53:41 EST 2010


Martin P. Hellwig wrote:
> Martin P. Hellwig wrote:
>> W. eWatson wrote:
>>> Maybe there's a more elegant way to do this. I want to express the 
>>> result of datetime.datetime.now() in fractional hours.
>>>
>>> Here's one way.
>>>
>>> dt=datetime.datetime.now()
>>> xtup = dt.timetuple()
>>> h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
>>> #  now is in fractions of an hour
>>
>> Here is another (though personally I don't find this more elegant than 
>> yours, perhaps a bit more readable):
>>
>>  >>> now = datetime.datetime.now()
>>  >>> fractional_hour = int(now.strftime('%H')) + 
>> int(now.strftime('%M')) / 60.0
>>
> Actually my version is overcomplicated:
>  >>> now = datetime.datetime.now()
>  >>> fractional_hour = now.hour + now.minute / 60.0
> 
See my post about the datetime controversy about 3-4 posts up from yours.



More information about the Python-list mailing list