From the course: Learning Python

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Using timedelta objects

Using timedelta objects - Python Tutorial

From the course: Learning Python

Using timedelta objects

- [Instructor] A common scenario involving dates and times involves performing mathematical operations on date and time values themselves. So for example, given a particular date, you might want to calculate a date in the future or the past relative to that date. And we can use the timedelta class in Python to help us with this. So that's what we're going to look at in this example here in my editor I'm going to open up timedeltas_start, and you can see I've already imported some of the modules and classes I'm going to need. So in order to use timedeltas, we first need to import the correct class from the daytime package. So let's do that first. So from datetime, I'm going to import the timedelta class, and a timedelta is basically a span of time. It's not a particular date, it's not a particular time, it's an amount of time, and you can use this class to perform time-based math. So let's take a look at some examples.…

Contents