1

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.

1
  • trimmed tags as the plotting is un-related to the question.
    – tacaswell
    Sep 8, 2013 at 19:36

1 Answer 1

4

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 
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.