Pergunta

I have a <type 'datetime.timedelta'> object of (datetime.timedelta(0, 7200) for 2:00:00 and I'm trying to convert that to 2.0 for two hours. Respectively if I have datetime.timedelta(0, 9000) for 2:30:00 I'd like it to return 2.5.

I don't even have example code because I can't even understand how I'd do such a conversion.

Foi útil?

Solução

If you are using python >= 2.7, you can use total_seconds:

hours = td.total_seconds() / 3600

If you are using python < 2.7, you can make your own total_seconds:

total_seconds = lambda td: (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top