Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. datetime.timedelta()

datetime.timedelta()

'timedelta' is an object that represents a duration of time — such as a number of days, hours, or seconds. You can add or subtract it from a datetime to calculate a future or past date, or subtract two datetime objects from each other to find the difference between them.

Syntax

from datetime import datetime, timedelta

# Create a duration object.
delta = timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds, weeks=weeks)

# Add a duration to a datetime to get a future datetime.
future = dt + delta

# Subtract a duration from a datetime to get a past datetime.
past = dt - delta

# Subtract two datetimes to get the difference as a timedelta.
diff = datetime_a - datetime_b

Constructor Arguments and Attributes

Argument / AttributeDescription
timedelta(days=n)Creates a duration object representing n days. Negative values are allowed.
timedelta(hours=n)Creates a duration object representing n hours.
timedelta(minutes=n)Creates a duration object representing n minutes.
timedelta(seconds=n)Creates a duration object representing n seconds.
timedelta(weeks=n)Creates a duration object representing n weeks (n × 7 days).
delta.daysReturns the days component of the duration.
delta.secondsReturns the seconds component of the duration (0–86399). Does not include days.
delta.total_seconds()Returns the total duration in seconds as a float. This is the sum of all days, hours, minutes, and seconds.

Sample Code

from datetime import datetime, timedelta

# Calculate the datetime 7 days from now.
now = datetime.now()
one_week_later = now + timedelta(weeks=1)
print(one_week_later.strftime('%Y-%m-%d'))  # Prints the date one week from today.

# Calculate the datetime 30 days ago.
thirty_days_ago = now - timedelta(days=30)
print(thirty_days_ago.strftime('%Y-%m-%d'))  # Prints the date 30 days ago.

# Combine multiple units in a single calculation.
deadline = now + timedelta(days=3, hours=5, minutes=30)
print(deadline.strftime('%Y-%m-%d %H:%M'))  # Prints the deadline datetime.

# Find the difference between two datetimes.
birthday = datetime(2000, 1, 1)
today = datetime.now()
diff = today - birthday
print(f'{diff.days} days have passed.')  # Prints the number of elapsed days.

# Get the total duration in seconds using total_seconds().
delta = timedelta(hours=2, minutes=30)
print(delta.total_seconds())  # Prints '9000.0' (2.5 hours × 3600 seconds).

# Calculate the time remaining until an event.
event = datetime(2025, 12, 31, 23, 59, 59)
remaining = event - datetime.now()
if remaining.days >= 0:
    print(f'{remaining.days} days until the event.')

# You can also perform arithmetic between two timedelta objects.
d1 = timedelta(days=5)
d2 = timedelta(days=3)
print(d1 + d2)          # Prints '8 days, 0:00:00'.
print(d1 - d2)          # Prints '2 days, 0:00:00'.
print(d1 * 2)           # Prints '10 days, 0:00:00'.

Notes

'timedelta' is produced automatically when you subtract two datetime objects. For example, the result of 'datetime_a - datetime_b' is a 'timedelta' object. This makes it straightforward to calculate things like the number of days since a birthday, the days remaining until an event, or the total duration of a task.

'timedelta.days' and 'timedelta.seconds' return the individual components of a duration. For example, 'timedelta(hours=25)' gives a 'days' value of '1' and a 'seconds' value of '3600'. When you need the total duration in seconds, always use 'total_seconds()' instead of calculating from 'days' and 'seconds' separately.

To create datetime objects, see 'datetime.datetime() / datetime.date() / datetime.time()'. For formatting and parsing, see 'datetime.strftime() / datetime.strptime()'.

If you find any errors or copyright issues, please .