- [Instructor] A common and somewhat frustrating scenario involving dates and times, involves performing mathematical operations on dates and times 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. We can use the time delta class in Python to help us with this, and that's what we're going to look at in this example. So in your editor, open up timedeltas_start, and in order to use timedeltas, we first need to import the correct packages.
So you can see that I'm already importing the datetime and date time classes, so now I'm going to write from datetime import timedelta. A timedelta is baseically a span of time. It's not a particular date, it's not a particular time, it's a span of time. And you can use this class to perform time-based mathematics. Let's take a look at some examples. To construct a basic timedelta, all you need to do is create the timedelta class and pass in the amount of time that you want the timedelta to represent.
So for example, I can write print, and then I'll create a timedelta, and I'll pass in days equals 365, and here I am using named parameters, which we saw earlier in the course. So I'll say days 365, hours equals five, and then minutes equals one. So in this case I'm creating a timedelta that represents a year, five hours and one minute. So let's print that out.
I'll save and I'll go to my debug view and run this. And I'll show the output window. And sure enough, there you can see, 365 days, five hours and one minute. Let's use today's date as a reference point for the next few operations. I'll create a reference to now, by using datetime.now. And then I'll just print that out for reference.
So we've got today's date and time. So let's use a timedelta to figure out what today's date will be one year from now. So I'll print one year from now it will be plus str now plus timedelta, and the days will be 365. All right, so let's try that.
And you see, it says today is, and here's today's date. And then one year from now it will be, and sure enough, there is the 2018 year. Okay, so let's keep on going, see what else we can do. Let's try using a timedelta with more than one argument. So I'll write print in two days and three weeks it will be, and then once again I'll write, str now plus timedelta.
And in this case I'll write days equals two, weeks equals three. Right, so I'm constructing a timedelta and then adding that delta to the current date. So let's save. And then let's run. And you can see that in two days and three weeks, it will be November 25th of 2017. By using a timedelta, I can calculate days in advance. Now let's try a day calculation in the past, using what we've already learned.
Once again, let's go back to the code. I'll write t equals datetime.now, minus timedelta. And I'll say weeks equals one. Then I'll format that using the strftime function that we looked at in the previous exercise, and I'll write percent A, percent B, percent d, comma, percent Y.
And I'll write print one week ago it was plus s. All right, so let's run that. And then you can see here, I've got some nicely formatted output now. So I have combined the timedelta calculation with the time formatting that we saw in the previous example, to see that one week ago it was Wednesday, November 1st, 2017.
Let's take a look at one more interesting example. Let's write a script that's going to calculate how many days it is until the next April Fool's Day. So here in the code, I'll start by getting today's date. So today equals date.today. Next I'll construct a new date that represents April Fool's Day. So I'll have a variable named afd equals date, I'm using the date constructor here to create a date, and I'll say today.year.
And I know that that's in April, so it's month four, on the first day. So now I'll compare the two dates to see if April Fool's Day has already gone by this year. Because if it has, then I need to get the date of the next April Fool's Day. So I'll write if afd is less than today. So you can see, I can use regular comparison operators to compare dates with each other. Colon, I'll say print April Fool's Day already went by, percent d days ago.
And I'll use that formatting string to print today minus afd.days. And then since it already went by, I have to say afd equals afd.replace. So now I'm going to replace the current April Fool's Day with the next one. And I'll simply say year equals today.year plus one.
So I'm replacing the current April Fool's Day date with a new one, and I'm calculating next year's April Fool's Day. Now I just need to subtract the April Fool's Day date from today to create a timedelta result. So I'll have a variable named time_to_afd equals afd minus today. And that creates a timedelta. And then I'll print It's just time_to_afd.days, and then I'll write, days until April Fool's Day.
All right, so that should be all there is to it. Let's go ahead and save this and run it. So you can see that April Fool's Day already went by, 221 days ago. And it's just 144 days until the next April Fool's Day. You can see that by using timedeltas, you can do some pretty advanced and complex date calculations.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML