datetime.timedelta() function - Python

Last Updated : 13 Jan, 2026

The datetime.timedelta() function in Python is used to represent a time difference. It allows you to add or subtract days, hours, minutes or seconds from a date or datetime object.

Example: This code gets the current date and time using datetime.now() and then uses timedelta(days=1) to calculate the date and time one day in the future.

Python
from datetime import datetime, timedelta
t = datetime.now()
res = t + timedelta(days=1)
print(res)

Output
2026-01-09 09:58:19.083495

Explanation:

  • datetime.now() gives the current date and time
  • timedelta(days=1) adds 1 day to the current time

Syntax

datetime.timedelta(days=0, seconds=0, minutes=0, hours=0, weeks=0)

Parameters:

  • days: number of days
  • seconds: number of seconds
  • minutes: number of minutes
  • hours: number of hours
  • weeks: number of weeks

Examples

Example 1: This example shows how to add days to the current date using timedelta().

Python
from datetime import datetime, timedelta
d = datetime.now()
f = d + timedelta(days=5)
print(f)

Output
2026-01-13 10:02:23.079919

Explanation: timedelta(days=5) adds 5 days to d

Example 2: This example demonstrates how to go back in time by subtracting hours.

Python
from datetime import datetime, timedelta
t = datetime.now()
p = t - timedelta(hours=2)
print(p)

Output
2026-01-08 08:03:36.816373

Explanation: timedelta(hours=2) subtracts 2 hours from t

Example 3: This example calculates the time difference between two datetime values.

Python
from datetime import datetime, timedelta
d1 = datetime.now()
d2 = d1 + timedelta(days=3)
print(d2 - d1)

Output
3 days, 0:00:00

Explanation: d2 - d1 returns a timedelta showing the time gap

Comment

Explore