We earn commission when you buy through affiliate links.

This does not influence our reviews or recommendations.Learn more.

When working with dates and times in Python, youll often need to calculate the time difference.

Article image

By the end of this tutorial, youll have learned:

Lets get started.

Thedatetimemodule is part of the Python standard library.

Or, install Python on your box.

Article image

As shown above, when you print the date, its formatted in theYYYY-MM-DDformat for dates.

Note: Be sure to specify the month as an integerwithoutanyleading zeros.

Its likely youre used to adding a leading zero to the month when writing down dates.

For example, try running the following code snippetwhere the month has been mentioned as03instead of just3.

How to create a time object in Python

To create a time object, lets import thetimeclass.

The following code snippet shows how you’re able to create a time objecttime1.

As with thedateobject, it’s possible for you to print out thetimeobject to see the formatted time.

And thetimeobject doesnt contain information about the date.

However, in practice, youll need both the date and time information.

So its recommended to use thedatetimeclass instead.

it’s possible for you to see that its set to0 the default value.

So far youve learned how to create timestamps in Pythonas dates, times, and datetimes.

Its now time to see how it’s possible for you to calculate the difference between any two timestamps.

Head over to the next section to find out.

How to use timedelta object in Python

In Python,timedeltadenotes a span of time.

Its the difference between twodate,time, ordatetimeobjects.

If you add or subtract twodate,time, ordatetimeobjects, youll get atimedeltaobject.

Thistimedeltaobject has useful attributes and methods that can help calculate the time difference.

Lets go ahead and calculate the difference between the twodatetimeobjectsdt1anddt2.

From the above code snippet, you’re free to see that thetdeltavariable holds the time difference betweendt1anddt2.

And the jot down oftdeltais verified to be of the classtimedeltausing Pythons built-intype()function.

Now lets code another example.

How to find time difference between two dates

Lets take a simple yet interesting example.

So lets use it gettodayour reference date.

In the code below, replacebdaywith your birthday to calculate the time left for your birthday this year.

If youre reading this after your birthday has passed, feel free to setbdayto your next birthday.

The next step is to calculatetime_diffwhich is atimedletaobject, as explained earlier.

Simply subtracttodayfrom yourbday, and youll have the time difference.

To calculate the total time difference in seconds, use thetotal_seconds()method on thetimedeltaobjecttime_diff.

Well, thats too long a wait!

That said, you now know how to calculate the time difference between any two timestamps in seconds.

Lets now revisit some basics and write down the following.

In the next sections, lets convert the time difference in seconds to minutes and hours.

Or you could divide the total seconds by 60*60 = 3600.

So youve now learned how to calculate the time difference in any unit of your choice.

Conclusion

In this tutorial, youve learned how to:

Hope you found this tutorial helpful.

Happy learning and coding!

Learn how to write equal to or not equal to code ormake a snake game in python here.