How to find time difference using Python?

Finding time differences in Python is straightforward using the datetime module and timedelta objects. A timedelta represents a duration ? the difference between two dates or times.

Understanding timedelta

The timedelta constructor accepts various time units as optional parameters ?

import datetime

# Creating a timedelta object
delta = datetime.timedelta(days=7, hours=2, minutes=30, seconds=45)
print(f"Duration: {delta}")
print(f"Total seconds: {delta.total_seconds()}")
Duration: 7 days, 2:30:45
Total seconds: 612645.0

Subtracting Time from Current Date

You can subtract a timedelta from a datetime to get an earlier time ?

import datetime

current_time = datetime.datetime.now()
print(f"Current time: {current_time}")

# Subtract 2 hours and 10 minutes
earlier_time = current_time - datetime.timedelta(hours=2, minutes=10)
print(f"Earlier time: {earlier_time}")
Current time: 2024-01-04 11:09:00.694602
Earlier time: 2024-01-04 08:59:00.694602

Finding Difference Between Two DateTime Objects

Subtracting two datetime objects returns a timedelta representing the difference ?

import datetime
import time

# Create two datetime objects
start_time = datetime.datetime.now()
time.sleep(2)  # Wait 2 seconds
end_time = datetime.datetime.now()

# Calculate difference
difference = end_time - start_time
print(f"Time difference: {difference}")
print(f"Type: {type(difference)}")
print(f"Difference in seconds: {difference.total_seconds()}")
Time difference: 0:00:02.001234
<class 'datetime.timedelta'>
Difference in seconds: 2.001234

Practical Example: Age Calculator

Calculate the age of a person in years, days, and hours ?

import datetime

# Birth date
birth_date = datetime.datetime(1990, 5, 15, 10, 30)
current_date = datetime.datetime.now()

# Calculate age
age_delta = current_date - birth_date

print(f"Birth date: {birth_date}")
print(f"Current date: {current_date}")
print(f"Age: {age_delta.days} days")
print(f"Age in years: {age_delta.days // 365} years")
print(f"Total seconds lived: {age_delta.total_seconds()}")
Birth date: 1990-05-15 10:30:00
Current date: 2024-01-04 11:09:00.694602
Age: 12287 days
Age in years: 33 years
Total seconds lived: 1061596140.694602

Working with Dates and Times

For time-only operations, convert to datetime first, perform calculations, then extract time ?

import datetime

# Working with time objects
time_obj = datetime.time(14, 30, 0)  # 2:30 PM
today = datetime.date.today()

# Combine date and time to create datetime
dt = datetime.datetime.combine(today, time_obj)
print(f"Combined datetime: {dt}")

# Add 3 hours
new_dt = dt + datetime.timedelta(hours=3)
print(f"After adding 3 hours: {new_dt.time()}")
Combined datetime: 2024-01-04 14:30:00
After adding 3 hours: 17:30:00

Conclusion

Use timedelta objects for time arithmetic and calculating differences between datetime objects. The total_seconds() method is useful for getting precise time differences in seconds.

Updated on: 2026-03-24T20:41:09+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements