Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total hours and minutes for timedelta in Python

How do I return or turn a timedelta, which is bigger than 24 hours, into an object containing the total hours and minutes (for example, 26:30) instead of "1 day, 2:30"?

like image 296
ThomasD Avatar asked Jan 12 '15 23:01

ThomasD


People also ask

How do I get hours from Timedelta object?

We can follow the same logic to convert a timedelta to hours. Instead of dividing the total_seconds() by the number of seconds in a minute, or dividing the timedelta object by timedelta(minutes=1) , we do it for hour.

How do I get Timedelta in Python?

timedelta() method. To find the difference between two dates in Python, one can use the timedelta class which is present in the datetime library. The timedelta class stores the difference between two datetime objects.


1 Answers

You can use total_seconds() to compute the number of seconds. This can then be turned into minutes or hours:

>>> datetime.timedelta(days=3).total_seconds() 259200.0 
like image 84
Simeon Visser Avatar answered Sep 28 '22 01:09

Simeon Visser