Python

Python Datetime.Timedelta

Python contains different packages that have modules for carrying out any task that we may comprehend. Python has a package called Timedelta that illustrates time. It is mostly employed to determine the time variation between times and dates. It is often applied to obtain the objects with slightly modified times and dates. Some mathematical operations are also performed by the timedelta() function. The datetime class in Python offers a variety of processes for working with days, durations, and periods. The date and datetime are components, so when we interact with these, we have to work with the object classes rather than bytes or times and dates. We have to import the datetime module before we handle the dates or times.

Syntax of Timedelta() Function

In the timedelta() function, the parameters given are optional. They contain the default values of zero. The integers and floats are the types of data utilized in all these parameters. The Timedelta() method returns a Date. The formatted() function of “timedelta” may provide a highly readable string illustration when the duration is specified as a string.

While evaluating the time frames in a sec, min, hrs, days, or years, the timedelta classes are commonly utilized to hold the distinction between the different date and time occurrences. This function’s constructor requires two parameters for the origin and termination timings as well as the parameter “precision” which can be used to specify the number of times the decimal places are included in the outcome.

We will examine many approaches in this article to observe how the timedelta() function of Python operates.

Example 1:

In this illustration, we will see how the timedelta() function works to determine a future date.

from datetime import datetime, timedelta

ini_time_for_now = datetime.now()

print ("current date:", str(ini_time_for_now))

future_date_after_4yrs = ini_time_for_now + \

timedelta(days = 1460)

future_date_after_4days = ini_time_for_now + \

timedelta(days = 4)

print('future date after 4 years:', str(future_date_after_4yrs))

print('future date after 4 days:', str(future_date_after_4days))

First, we import the two classes – datetime and timedelta – from the datetime package. Then, we declare a variable. Along with this, we use the datetime.now() function to get the existing date and time of the system. To print this existing date, we utilize the print() function. Within this function, we call the str() method. This method contains the existing time and date. So, we specify the number of years and number of days, respectively.

To find the future date after 4 years, we call the timedelta() method. This method has the parameter of the days, and its value is 1460. Then, to find the future date after 4 days, we utilize the timedelta() function again. Now, we pass the day 4 as the attribute of the function. We invoke the print() function to display both future dates after 4 years and after 4 days.

Example 2:

In this case, we learn how the timedelta() function works to get the past date that was a certain number of hours or years ago.

from datetime import datetime, timedelta

ini_time_for_now = datetime.now()

print ('current date:', str(ini_time_for_now))

past_date_before_4yrs = ini_time_for_now - \

timedelta(days = 1460)

past_date_before_4hours = ini_time_for_now - \

timedelta(hours = 4)

print('past date before 4 years:', str(past_date_before_4yrs))

print('past date before 4 hours:', str(past_date_before_4hours))

First, two classes from the datetime library, datetime and timedelta, are integrated. A package that exhibits the time duration between the given intervals is called timedelta. The package provides functionality that lets the users figure out how much time has passed between the multiple time points in terms of secs, mins, hrs, and days. The datetime framework is also available in Python and can be used to handle dates. The timedelta module of the datetime package makes it simple to calculate the time frames.

We can do the mathematical operation (addition or subtraction) from the date class with the help of the timedelta() function. Then, a variable is defined. Additionally, we utilize the datetime.now() method to retrieve the system’s current date and time. Then, we call the print() method to output this current date. We use the str() technique inside of this function. This procedure incorporates the current time and date.

We want to compute the previous dates. Therefore, we provide the years and days, correspondingly. We use the timedelta() function to locate the dates in the past that are less than 4 years before. The day argument for this method has a value of 1460. Then, we call the timedelta() function once more to determine the previous date that was before 4 hours. Now, we provide the function with the value of hour 4 as a parameter. To show the dates that are earlier than 4 years and 4 hours, we may use the print() function.

Example 3:

The timedelta() function can be used to find the time difference between two specified dates. Let’s see how it works.

from datetime import datetime, timedelta

existing_date = datetime.now()

print('The existing date is:', existing_date)

upcoming_date = existing_date + \

timedelta(days = 5)

print ("The upcoming day:", upcoming_date)

print('The time variation in given days:', upcoming_date - existing_date)

At the start of the program, two classes, datetime and timedelta, are included from the header file datetime. In the next line, we get the existing date by using the datetime.now() method. We have an “existing_date” variable that is used to store the existing date of the computer. The print() method is called to print this date. We want to determine the future date, so the timedelta()function is called.

Here, we define the value of the “days” parameter to 5 because we want to get the date after 5 days. The print() method prints the new future date on the screen. In the last line, we use the print() statement again to show the time variation between the existing date and the future date.

Conclusion

In this article, we discussed the timedelta() method which is a part of the datetime module and could be utilized to adjust the dates, identify the dates throughout the past and future, and evaluate the dates’ variations. It provides one of the most basic techniques for handling the dates. The timedelta() method was used in an illustration to predict the dates in the past and future. By the use of the timedelta() method, we previously determined the distinction between the different dates.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content