Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python DateTime – Timedelta Class

  • Last Updated : 19 Oct, 2021

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

Syntax:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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)
  
# Sutracting 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 NameDescription
minminimum value of timedelta object is -999999999
maxmaximum value of timedelta object is 999999999
resolutionThe 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

OperatorDescription
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)
  
# Negatiob 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!