The Wayback Machine - https://web.archive.org/web/20240710044939/https://www.geeksforgeeks.org/python-datetime-timedelta-class/
Open In App

Python DateTime – Timedelta Class

Last Updated : 22 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Timedelta class is used for calculating differences between dates and represents a duration. The difference can both be positive as well as negative.

Syntax:

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

Example:

Python3




# Timedelta function demonstration
 
from datetime import datetime, timedelta
 
# creating datetime objects
date1 = datetime(2020, 1, 3)
date2 = datetime(2020, 2, 3)
 
# difference between dates
diff = date2 - date1
print("Difference in dates:", diff)
 
# Adding days to date1
date1 += timedelta(days = 4)
print("Date1 after 4 days:", date1)
 
# Subtracting days from date1
date1 -= timedelta(15)
print("Date1 before 15 days:", date1)


Output

Difference in dates: 31 days, 0:00:00
Date1 after 4 days: 2020-01-07 00:00:00
Date1 before 15 days: 2019-12-23 00:00:00

Class Attributes:

Let’s see the attributes provided by this class – 

Attribute Name Description
min minimum value of timedelta object is -999999999
max maximum value of timedelta object is 999999999
resolution The minimum possible difference between timedelta objects

 

Example: Getting the minimum and maximum value of timedelta objects

 

Python3




from datetime import timedelta
 
# Getting minimum value
Min = timedelta.min
print("Minimum value of timedelta object", Min)
 
# Getting minimum value
Max = timedelta.max
print("Maximum value of timedelta object", Max)


Output

Minimum value of timedelta object -999999999 days, 0:00:00
Maximum value of timedelta object 999999999 days, 23:59:59.999999

Output

Minimum value of timedelta object -999999999 days, 0:00:00

Maximum value of timedelta object 999999999 days, 23:59:59.999999

Class Functions

Timedelta class provides only one function which is total_seconds(). This method returns the duration provided by the timedelta object in the number of seconds. 

Note: For a duration of more than 270 years this method will be accurate for microseconds.

Example: Getting various duration in seconds

Python3




from datetime import timedelta
 
# Getting minimum value
obj = timedelta(hours=1)
print(obj.total_seconds())
 
obj = timedelta(minutes=1)
print(obj.total_seconds())
 
obj = timedelta(days=1)
print(obj.total_seconds())


Output

3600.0
60.0
86400.0

Operations supported by Timedelta Class

Operator Description
Addition (+) Adds and returns two timedelta objects
Subtraction (-) Subtracts and returns two timedelta objects
Multiplication (*) Multiplies timedelta object with float or int 
Division (/) Divides the timedelta object with float or int
Floor division (//) Divides the timedelta object with float or int and return the int of floor value of the output 
Modulo (%) Divides two timedelta object and returns the remainder
+(timedelta) Returns the same timedelta object
-(timedelta) Returns the resultant of -1*timedelta
abs(timedelta) Returns the +(timedelta) if timedelta.days > 1=0 else returns -(timedelta)
str(timedelta) Returns a string in the form (+/-) day[s],  HH:MM:SS.UUUUUU
repr(timedelta) Returns the string representation in the form of the constructor call

Example 1: Performing basic arithmetic operations on timedelta objects.

Python3




from datetime import timedelta
 
# creating the timedelta object
t1 = timedelta(days=1)
print("Original timedelta:", t1)
 
# multiplication
t2 = t1*5.5
print("After Multiplication:", t2)
 
# Subtraction
res = t2 - t1
print("After Subtraction:", res)
 
# addition
res += t2
print("After Addition:", res)
 
# division
res = t2/2.5
print("After division:", res)
 
# floor division
res = t2 //2
print("After floor division:", res)
 
# Modulo
res = t2%timedelta(days=3)
print("After Modulo:", res)


Output

Original timedelta: 1 day, 0:00:00
After Multiplication: 5 days, 12:00:00
After Subtraction: 4 days, 12:00:00
After Addition: 10 days, 0:00:00
After division: 2 days, 4:48:00
After floor division: 2 days, 18:00:00
After Modulo: 2 days, 12:00:00

Example 2: Getting Absolute value and the string representation of timedelta objects

Python3




from datetime import timedelta
 
# creating the timedelta object
t1 = timedelta(days=1)
print("Original timedelta:", t1)
 
# Negation of timedelta object
t1 = -(t1)
print("After Negation:", t1)
 
# Getting Absolute value
t1 = abs(t1)
print("Absolute Value:", t1)
 
# Getting string representation
print("String representation:", str(t1))
 
# Getting Constructor call
print("Constructor call:", repr(t1))


Output

Original timedelta: 1 day, 0:00:00
After Negation: -1 day, 0:00:00
Absolute Value: 1 day, 0:00:00
String representation: 1 day, 0:00:00
Constructor call: datetime.timedelta(1)

Note: For more information on Python Datetime, refer to Python Datetime Tutorial



Previous Article
Next Article

Similar Reads

Python | datetime.timedelta() function
Python timedelta() function is present under datetime library which is generally used for calculating differences in dates and also can be used for date manipulations in Python. It is one of the easiest ways to perform date manipulations. Syntax : datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) Ret
4 min read
Using Timedelta and Period to create DateTime based indexes in Pandas
Real life data often consists of date and time-based records. From weather data to measuring some other metrics in big organizations they often rely on associating the observed data with some timestamp for evaluating the performance over time. We have already discussed how to manipulate date and time in pandas, now let's see the concepts of Timesta
3 min read
Python DateTime - DateTime Class
DateTime class of the DateTime module as the name suggests contains information on both dates as well as time. Like a date object, DateTime assumes the current Gregorian calendar extended in both directions; like a time object, DateTime assumes there are exactly 3600*24 seconds in every day. But unlike the date class, the objects of the DateTime cl
5 min read
Python | Pandas Timedelta.floor()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Python | Pandas Timedelta.asm8
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Python | Pandas Timedelta.components
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
2 min read
Python | Pandas Timedelta.days
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Python | Pandas Timedelta.delta
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Python | Pandas Timedelta.nanoseconds
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Python | Pandas Timedelta.microseconds
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Timedelta is a subclass of datetime.timedelta, and behaves in a similar manner. It is the pandas equivalent of python’s datetime.timedel
1 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg