Suppose you want to find the future date or the past date quickly. Of course, there can be many ways to find it. But the quickest way to find the future and past date is the Python timedelta. In this entire tutorial, you will know how to use timedelta in python with steps.
Steps to implement Python Timedelta
In this entire section, you will know all the steps to implement python timedelta. Follow all the steps to correctly implement timedelta.
Step 1: Import all the necessary libraries
The first step is to import all the required libraries to implement python timedelta. You can import the library using the import statement.
from datetime import datetime
from datetime import timedelta
Step 2: Find today’s date and time
In order to implement the python timedelta you have to find the base time. For example, I am using the present time and date as the base time. In python, you can find today’s date and time using the now() method. Execute the following code to find it.
now = datetime.now()
print(now)
Output
![Present date and time in python](https://i0.wp.com/www.datasciencelearner.com/wp-content/uploads/2021/05/Present-date-and-time-in-python.jpg?resize=309%2C82&ssl=1)
Step 3: Implement the examples for the python timedelta
After the above steps let’s implement the examples on finding the past and the future date.
Examples on Finding the future date
Let’s find the date from now after 365 days. To do so you have to use the timedelta(days=365).
print(str(now + timedelta(days=365)))
Output
![Date after one year from now](https://i0.wp.com/www.datasciencelearner.com/wp-content/uploads/2021/05/Date-after-one-year-from-now.jpg?resize=339%2C91&ssl=1)
Find the date after two weeks. Again to find you have pass the weeks=2 as an argument inside the timedelta() method. Execute the code below.
print(str(now + timedelta(weeks=2)))
Output
![Date two weeks from now](https://i0.wp.com/www.datasciencelearner.com/wp-content/uploads/2021/05/Date-two-weeks-from-now.jpg?resize=314%2C74&ssl=1)
Examples on Finding the past date
In this same way, you can find the past date using the timedelta. To do so you have to just subtract the current time.
For example, let’s calculate the time one year ago. Then you have to subtract the current time with timedelta(days=365).
print(str(now - timedelta(days=365)))
Output
![Date one year ago from now](https://i0.wp.com/www.datasciencelearner.com/wp-content/uploads/2021/05/Date-one-year-ago-from-now.jpg?resize=308%2C77&ssl=1)
Now let’s find the date two weeks ago. Then you have to subtract timedelta(weeks=2) from the now. Execute the below code and see the output.
print(str(now - timedelta(weeks=2)))
Output
![Date two weeks ago from now](https://i0.wp.com/www.datasciencelearner.com/wp-content/uploads/2021/05/Date-two-weeks-ago-from-now.jpg?resize=331%2C82&ssl=1)
Conclusion
The timedelta() method is the quickest way to manipulate date and time in python. These are the examples on timedelta. Hope you have liked this tutorial. If you have any queries then you can contact us for more information.
Source:
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.