Linux Hint Posted July 1, 2023 Share Posted July 1, 2023 Python can handle various types of data and operations. It provides various functions and modules to work with dates and times. The module called “datetime” is one such module that provides several classes and functions to deal/work with the date and time objects in Python. This module is utilized for various operations such as determining the difference between particular dates, adding or removing a certain amount of time from a date, or date formatting in a specific way. This write-up will present a detailed guide on Python “datetime.timedelta()” function by covering the following content: What is the “datetime.timedelta()” Function in Python? Adding the Current Date With the New Date Using the “timedelta()” Function. Subtracting the Current Date From the New Date Using the “timedelta()” Function. Determining the Time Difference Between Two Specified DateTime Objects. What is the “datetime.timedelta()” Function in Python? In Python, the “datetime.timedelta()” function is utilized to create a “timedelta” object, which symbolizes time duration. It can be utilized to determine the difference between two dates or times or to manipulate dates and times in other ways. Syntax datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0) In this syntax, all the parameters are not mandatory and their default value is “0”. The parameter value may be integers or floats and can be positive (+) or negative (-). Return Value The “datetime.timedelta()” function in Python returns a “datetime.timedelta” object which represents/describes a time duration. Example 1: Adding the Current Date With the New Date Using the “datetime.timedelta()” Function The following code is utilized to add a current date to the new date with the help of the “datetime.timedelta()” function: import datetime time1 = datetime.datetime.now() print ("Current 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)) In the above code: The “datetime” module is imported and the “datetime.now()” function retrieves the current date and time and stores it in a variable named “time1” The “timedelta()” function is applied to create the specified “timedelta” objects such as “new_date” and “new_date1” with stated days value. The “timedelta” objects are then added/inserted to the specific current date and time in both cases by utilizing the concatenation operator “+”. Output The particular “timedelta” object has been added to the current date in both cases. Example 2: Subtracting the Current Date From the New Date Using the “datetime.timedelta()” Function The below code is utilized to subtract the current date from the new date utilizing the discussed function: import datetime time1 = datetime.datetime.now() print ("Current 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)) In the above code lines: The “datetime” module is imported and the “datetime.now()” function is utilized to determine the existing/current date and time. Similarly, the “timedelta()” function is used to create specified “timedelta” objects named “new_date” and “new_date1” with the stated days and hours. The specified “timedelta” objects are subtracted from the current date and time by utilizing the “subtraction operator”. Output The particular “timedelta” objects have been subtracted from the current time. Example 3: Determining the Time Difference Between the Two Specified DateTime Objects The given code is utilized in Python to determine the time difference between the two specified “datetime” objects: import datetime time1 = datetime.datetime.now() print ("Current Date: ", str(time1)) new_date = time1 + datetime.timedelta(days = 365, hours=14) print('\nNew Date:', str(new_date)) date_diff = new_date - time1 print('\nDifference Between Date:', str(date_diff)) In the above block of code: The “datetime.now()” function gets the current date and time and assigns it to the “time1” variable. The “timedelta()” function creates the DateTime object and appends it to the current “datetime” value using the “+” operator. The difference between the current and new date has been calculated by utilizing the subtraction operator. Output The difference between two specified “DateTime” objects has been determined. Conclusion The “timedelta()” function of the “datetime” module is utilized to create a “timedelta” object representing the time period. This function can be used with the “datetime.now()” to get the difference between the current and specified “DateTime” object. It can also manipulate dates and times such as inserting or subtracting particular dates and times. This write-up provided a comprehensive tutorial on Python’s “datetime.timedelta()” function utilizing several examples. View the full article Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.