Python timedelta is representing the duration of the difference between two dates, times, or datetime objects. A timedelta is a class and part of datetime modules. In this tutorial, you will understand the timedelta function with examples.
Using a timedelta object in python is easy because it supports mathematical operations (Arithmetic operation), such as addition, multiplication, subtraction, etc.
Before you start if you don’t have knowledge about Python datetime module then must read the following topics :
Syntax
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
The default value of all arguments is 0 and also optional. The Arguments value can be integers (numbers) or floats and may be positive or negative.
Python timedelta example
You have to import class datetime and timedelta from the in-built datetime module for this example. In this example, we are adding 365 days to the current date.
from datetime import datetime, timedelta current_datetime = datetime.now() print("Current date ", current_datetime) # date after 365 days future_date = current_datetime + timedelta(days=365) print('Date After 365 days from current Date:', future_date)
Output:
Difference between two Date and Time
We are an import-only datetime module in this example. Using a timedelta class to get the difference between days and times.
import datetime current_time = datetime.timedelta(days=3, hours=25, minutes=24) end_time = datetime.timedelta(days=4, hours=30, minutes=26) diff_time = end_time - current_time print('Current time :', current_time) print('End time : ', end_time) print('Difference : ', diff_time)
Output: Current time : 4 days, 1:24:00
End time : 5 days, 6:26:00
Difference : 1 day, 5:02:00
Convert a timedelta to minutes
Here is an example how to get minutes using timedelta.
import datetime current_time = datetime.timedelta(hours=29, minutes=24) end_time = datetime.timedelta(hours=30, minutes=26) diff_time = end_time - current_time minutes = diff_time.total_seconds() / 60.0 print('minutes :', minutes)
Output: minutes : 62.0
Python timedelta attributes
Here is timedelta class attributes are:
timedelta.min
– The most negative timedelta object, its value is – timedelta(-999999999).timedelta.max
– The most positive timedelta object, like this – timedelta(days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999).timedelta.resolution
– The smallest possible difference between non-equal timedelta objects, timedelta(microseconds=1).
from datetime import timedelta print(timedelta.min) print(timedelta.max) print(timedelta.resolution)
Output: -999999999 days, 0:00:00
999999999 days, 23:59:59.999999
0:00:00.000001
Reference: https://docs.python.org/3/library/datetime.html (Official website)
Do comment if you have any doubt and suggestion on this tutorial.
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.13.6Python 3.7
All examples Difference between two Date, Time,
etc used in timedelta python 3, so it may change its different from python 2 or upgraded versions.