Python can deal with numerous varieties of information and operations. It offers numerous features and modules to work with dates and occasions. The module known as “datetime” is one such module that gives a number of lessons and features to deal/work with the date and time objects in Python. This module is utilized for numerous operations akin to figuring out the distinction between explicit dates, including or eradicating a sure period of time from a date, or date formatting in a particular manner.
This write-up will current an in depth information on Python “datetime.timedelta()” perform by overlaying the next content material:
What’s the “datetime.timedelta()” Perform in Python?
In Python, the “datetime.timedelta()” perform is utilized to create a “timedelta” object, which symbolizes time length. It may be utilized to find out the distinction between two dates or occasions or to control dates and occasions in different methods.
Syntax
datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
On this syntax, all of the parameters usually are not obligatory and their default worth is “0”. The parameter worth could also be integers or floats and might be constructive (+) or unfavorable (-).
Return Worth
The “datetime.timedelta()” perform in Python returns a “datetime.timedelta” object which represents/describes a time length.
Instance 1: Including the Present Date With the New Date Utilizing the “datetime.timedelta()” Perform
The next code is utilized so as to add a present date to the brand new date with the assistance of the “datetime.timedelta()” perform:
time1 = datetime.datetime.now()
print (“Present Date: “, str(time1))
new_date = time1 + datetime.timedelta(days = 365)
print(‘nNew Date:’, str(new_date))
new_date1 = time1 + datetime.timedelta(days = 10)
print(‘nNew Date1:’, str(new_date1))
Within the above code:
- The “datetime” module is imported and the “datetime.now()” perform retrieves the present date and time and shops it in a variable named “time1”
- The “timedelta()” perform is utilized to create the required “timedelta” objects akin to “new_date” and “new_date1” with said days worth.
- The “timedelta” objects are then added/inserted to the particular present date and time in each instances by using the concatenation operator “+”.
Output
The actual “timedelta” object has been added to the present date in each instances.
Instance 2: Subtracting the Present Date From the New Date Utilizing the “datetime.timedelta()” Perform
The under code is utilized to subtract the present date from the brand new date using the mentioned perform:
time1 = datetime.datetime.now()
print (“Present Date: “, str(time1))
new_date = time1 – datetime.timedelta(days = 365, hours=14)
print(‘nNew Date:’, str(new_date))
new_date1 = time1 – datetime.timedelta(days = 10, minutes=30, hours=5)
print(‘nNew Date1:’, str(new_date1))
Within the above code strains:
- The “datetime” module is imported and the “datetime.now()” perform is utilized to find out the present/present date and time.
- Equally, the “timedelta()” perform is used to create specified “timedelta” objects named “new_date” and “new_date1” with the said days and hours.
- The desired “timedelta” objects are subtracted from the present date and time by using the “subtraction operator”.
Output
The actual “timedelta” objects have been subtracted from the present time.
Instance 3: Figuring out the Time Distinction Between the Two Specified DateTime Objects
The given code is utilized in Python to find out the time distinction between the 2 specified “datetime” objects:
time1 = datetime.datetime.now()
print (“Present Date: “, str(time1))
new_date = time1 + datetime.timedelta(days = 365, hours=14)
print(‘nNew Date:’, str(new_date))
date_diff = new_date – time1
print(‘nDistinction Between Date:’, str(date_diff))
Within the above block of code:
- The “datetime.now()” perform will get the present date and time and assigns it to the “time1” variable.
- The “timedelta()” perform creates the DateTime object and appends it to the present “datetime” worth utilizing the “+” operator.
- The distinction between the present and new date has been calculated by using the subtraction operator.
Output
The distinction between two specified “DateTime” objects has been decided.
Conclusion
The “timedelta()” perform of the “datetime” module is utilized to create a “timedelta” object representing the time interval. This perform can be utilized with the “datetime.now()” to get the distinction between the present and specified “DateTime” object. It might probably additionally manipulate dates and occasions akin to inserting or subtracting explicit dates and occasions. This write-up offered a complete tutorial on Python’s “datetime.timedelta()” perform using a number of examples.