Convert timedelta to minutes in Python

This article will discuss how we can convert the datetime module’s timedelta object to total minutes.

A little background of the problem in hand,

The datetime.timedelta in the datetime module represents the difference between two dates, time, or datetime objects. For example, we have two timestamps as datetime objects,

from datetime import datetime

start_timestamp =   datetime(2021, 10, 1, 7, 20, 17, 100000)
end_timestamp   =   datetime(2021, 10, 1, 8, 25, 12, 200002)

If you want the difference between these two timestamps, you can subtract these datetime objects. It will give a timedelta i.e.

# Get different between two datetime as timedelta object.
timedelta_obj = (end_timestamp - start_timestamp)

print(timedelta_obj)
print( type(timedelta_obj) )

Output:

1:04:55.100002
<class 'datetime.timedelta'>

This timedelta object contains the time difference between two datetimes. But when we printed it, the absolute difference gets printed in hh:mm::ss.ffffff format.

What if we want the absolute difference in total minutes only? For that we can convert this timedelta object to minutes only. Let’s see how to do that,

Convert timedelta to minutes in python

The timedelta object represents a duration. It has a function total_seconds(), which gives the total seconds in the complete duration of timedelta object. We can get the whole seconds from it and then divide it by 60 to get the entire duration in minutes only. For example,

# Convert timedelta object to minutes
diff_in_minutes = timedelta_obj.total_seconds() / 60

print("Total Time Difference : {} Minutes".format(diff_in_minutes) )

Output:

Total Time Difference : 64.91833336666667 Minutes

We got the difference between two timestamps in minutes only by converting timedelta to minutes.

It contains the decimal part too. If you want approx absolute value, then you can round off the minutes value i.e.

# Convert timedelta object to minutes
diff_in_minutes = timedelta_obj.total_seconds() / 60
# Round of the minutes value
diff_in_minutes = round(diff_in_minutes)

print("Total Time Difference : {} Minutes".format(diff_in_minutes) )

Output

Total Time Difference : 65 Minutes

The complete working example is as follows,

from datetime import datetime

start_timestamp =   datetime(2021, 10, 1, 7, 20, 17, 100000)
end_timestamp   =   datetime(2021, 10, 1, 8, 25, 12, 200002)

# Get different between two datetime as timedelta object.
timedelta_obj = (end_timestamp - start_timestamp)

print(timedelta_obj)
print( type(timedelta_obj) )

# Convert timedelta object to minutes
diff_in_minutes = timedelta_obj.total_seconds() / 60

print("Total Time Difference : {} Minutes".format(diff_in_minutes) )

# Round of the minutes value
diff_in_minutes = round(diff_in_minutes)

print("Total Time Difference : {} Minutes".format(diff_in_minutes) )

Output:

1:04:55.100002
<class 'datetime.timedelta'>
Total Time Difference : 64.91833336666667 Minutes
Total Time Difference : 65 Minutes

Summary:

Today we learned that how we can convert datetime.timedelta to minutes in python.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top