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. Commented 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 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.