Datetime timedelta

A DateTime is a library in python which is used for date and time calculations. And a timedelta is a class that can be used to determine the difference between 2 different dates, years, months, weeks, days, hours, minutes, seconds, and microseconds.

By using DateTime library and timedelta class, we can check the current day, date and time, future day, date and time, and past day, date, and time.

Syntax

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0,minutes=0, hours=0, week=0)

In the above syntax, the parameters are optional you can use 1 parameter at a time or all together as you want.

Example 1

from datetime import datetime, timedelta

# for checking current time
current_time_now = datetime.now()
 
# printing current_date
print ("current_date", str(current_time_now))
 
# determining future dates after two years

futuredate_for3years = current_time_now + \
                        timedelta(days = 1095)

#determining future dates after two days

futuredate_for4days = current_time_now + \
                         timedelta(days = 4)
 
# printing calculated future_dates
print('future date of 3 years is:', str(futuredate_for3years))
print('future date of 4 days is:', str(futuredate_for4days))

Code Explanation

In the above code, we imported the library first then for checking the current time we used the datetime.now() function which will show the initial time and date.

Then we printed both the current time and date, after that for calculating the future dates after two years we added the current time with timedelta function and passed the total number of days of 3 years as a parameter in it which means we added the current time with the total 1095 days of 3 years.   

We followed the same steps for finding out the future date after two days, Just added the current time and 4 days.

In the end, we printed the future days and time after 3 years and the future days and time after 4 days. checkout the results below.

Output

current_date 2022-07-17 19:50:22.343980
future date of 3 years is: 2025-07-16 19:50:22.343980
future date of 4 days is: 2022-07-21 19:50:22.343980

you can see the current date and time, future date and time after 3 years, and future date and time after 4 days.

You can calculate the past years and past days’ date and time as well just subtract the current day time from the total days of past years.

Let’s move forward to our next example.

Example 2

# importing Timedelta function from the library
from datetime import datetime, timedelta

#initial time
ini_date_time = datetime.now()

# printing current_date
print ("currentdate", str(ini_date_time))

# Some another datetime
final_date_time = ini_date_time + \
                
timedelta(days = 3)

# printing final time, date
print ("final date and time", str(final_date_time))


# printing calculated past_dates
print('Time difference:', str(final_date_time - \
                             ini_date_time))

Code Explanation

In the above code we imported the datetime library first then we printed the current date 

By using datetime().now function and then we determined the final date and time after 3 days from today.

Then we find out the difference between both dates and times by subtracting the initial date and time from the final date and time.

Basically, the above code will show the current and future dates and times and will also show the time difference between them. 

Check out the results below.

Output

currentdate 2022-07-17 16:18:28.260951
final date and time 2022-07-20 16:18:28.260951
Time difference: 3 days, 0:00:00

Conclusion

In this tutorial, we talked about the datetime and timdelta function in python. We discussed the uses of date and timedelta. We also discussed their examples.

In the first examples we saw how to determine the current day, time, and future day, time by using the daytime.now() and timedelta function.

In the other example, we saw how can we determine the current day, time, and future day, time, and the difference between them.

Hope you like the tutorial if you have any queries regarding this article so feel free to contact us below.Thanks for reading.

Suggested Article:

Leave a Comment