Python Timedelta Know How to use it with examples

Python Timedelta : Know How to use it with examples

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
Present date and time in python

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
Date after one year from now

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
Date two weeks from now

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
Date one year ago from now

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
Date two weeks ago from now

 

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:

Timedelta Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner