timedelta()
DateTimeRepresents a duration — the difference between two dates or times.
Signature
timedelta(days, seconds, microseconds, ...)
Returns
timedeltaExample
from datetime import datetime, timedelta now = datetime.now() tomorrow = now + timedelta(days=1) last_week = now - timedelta(weeks=1)
About timedelta()
timedelta is a Python datetime function with the signature timedelta(days, seconds, microseconds, ...). Represents a duration — the difference between two dates or times. It returns a value of type timedelta.
Python provides a rich set of built-in functions and standard library modules that cover common programming tasks. Understanding these functions helps you write more idiomatic, efficient Python code. The timedeltafunction is commonly used in data processing, web development, scripting, and automation tasks.
When working with timedelta(), consider edge cases like empty inputs, None values, and type mismatches. Python's duck typing means many built-in functions work with any object that implements the required protocol (e.g., __len__ for len(), __iter__ for iteration). This flexibility is a key strength of Python's design philosophy.