1. Math with Dates
In the last lesson, we discussed how to create date objects and access their attributes. In this lesson, we're going to talk about how to do math with dates: counting days between events, moving forward or backward by a number of days, putting them in order, and so on.
2. Math with dates
Let's take a step back. Think back to when you first learned arithmetic. You probably started with something like this: a number line. This one has the numbers 10 through 16 on it. A number line tells you what order the numbers go in, and how far apart numbers are from each other.
Let's pick two numbers, 11 and 14, and represent them in Python as the variables a and b, respectively. We'll put them into a list, l.
Python can tell us which number in this list is the least, using the min() function. min stands for the minimum. In this case, 11 is the lowest number in the list, so we get 11.
3. Math with dates
We can also subtract numbers. When you subtract two numbers, in this case subtracting 11 from 14, the result is 3. Said another way, if we took three steps from 11, we would get 14.
4. Math with dates
Now let's think about how this applies to dates.
Let's call this line a calendar line, instead of a number line. Each dot on this calendar line corresponds to a particular day.
5. Math with dates
Let's put two dates onto this calendar line: November 5th, 2017, and December 4th, 2017.
Let's represent this in Python. We start by importing the date class from the datetime package.
We create two date objects: d1 is November 5th, 2017, and d2 is December 4th, 2017. As before, we put them into a list, l.
What Python is doing under the hood, so to speak, is not that different from putting the dates onto a calendar line.
For example, if we call min of l, we again get the "least" date, which means the earliest one. In this case, that's November 5th, 2017.
6. Math with dates
And just like numbers, we can subtract two dates. When we do this, we get an object of type "timedelta". Timedeltas give us the elapsed time between events. If you access the days attribute of this object, you can obtain the number of days between the two dates.
7. Math with dates
We can also use a timedelta in the other direction. First, let's import timedelta from datetime.
Next, we create a 29-day timedelta, by passing days=29 to timedelta(). Now when we add td to our original date we get back December 4th, 2017. Python handled the fact that November has 30 days in it for us, without us having to remember which months are 30 day months, 31 day months, or 28 day months.
8. Incrementing variables with +=
Finally a quick side note: we will use the "plus-equals" operation a number of times in the rest of the course, so we should discuss it. If you aren't familiar with it, you can see how it works here.
On the left-hand side, we create a variable x, set it to zero. If we set x equal to x + 1, we increment x by 1. Similarly, on the right-hand side, we set x = 0, and then we increment it with x += 1. It has the same effect, and we'll use it all the time for counting.
9. Let's Practice!
We talked about how date objects are very similar to numbers, and how you can subtract them to get a timedelta, or add a timedelta to a date to get another date. We also briefly touched on the += operator. It's time for you to practice these concepts.