It's very common to have to work with dates when you're scripting.
You might have a lot of data that has dates attached to it and
you need to sort them or compare them or something like that.
Python provides a date time module
that makes it a little bit easier to work with such dates.
Okay. And the daytime module is pretty complex.
It deals with dates,
it deals with times,
it can handle time zones and so on.
We're gonna work with a small subset of the daytime module that's much
simpler and we're just going to look at
dates and we're not going to worry about time zones.
So let's see some of the things that we can do with the daytime module.
As you know in Python whenever we want to use a module we first have to
import it so you can see up here at the top of the file we have import datetime.
Datetime is the name of the module that Python provides.
This is a built in module that you can always use with in Python.
Once we've imported it we can now use it. All right.
And so first let's see.
We might wanna be able to actually create some dates.
Okay. In order to create a date.
You need to pass a year,
a month and a day and then that will create a date object for you,
if you pass that to the datetime.datefunction.
Okay. So you can see here, date1,
I have datetime.date (1999, 12,
31) So that's December 31st 1999.
Right. Last day of the last century.
We similarly have date2 here,
with (2000, 1, 1).
So January 1st 2000.
The first day of the century.
And we can create more dates.
And we can even create a date that corresponds today.
Alright. daytime.date.today is the function that allows me to get today's date.
This is important because you don't necessarily know when
today's date is when you're actually running the program.
Right. You don't hard code in today's date.
Righ? You want that to be- you
know the current day whenever you actually run the program.
And you might wanna do this to print something out,
to find out a difference between- you know
today's date and some date in the past or whatever.
Okay. But first, like I said we need to be able to create these dates.
So let's run this program and we've created these three, actually four dates.
Three dates and today let's print them out.
Okay. And you can see that Python's daytime module prints them
out with the year dash month dash day format.
Okay. And I didn't print it today.
Why? I wanna keep you guessing.
When did I actually shoot this video.
Maybe it's today. All right.
Okay. So you can see how we can create these dates.
We can print them out. All right.
Not terribly useful but we'll see what we can do with them in a second.
Now I want to point this one thing out here that you
know I could actually pass invalid numbers here.
Okay. So let's say I want to past (2012, 8,32).
So the 32nd day of August.
We hopefully know that doesn't exist. Let's see what happens.
All right. That creates an error and you get the message,
Value error: day must be in 1..31.
Actually it's more strict than that right.
Day has to actually be valid for the particular month that you've passed in. Okay.
All right so let's comment that out again so that we don't
have a broken program and run it make sure we did that correctly.
Okay. All right.
Now once I have dates,
I probably want to do something with them.Okay`.
Python allows you to actually compare dates to each
other using all of the comparison operators that we've talked about before.
Less than, greater than, less than or equal to,
an equal to equal to,
or not equal to.
So I can use any of these six and they behave pretty much as you expect less
than is true if you know the first date occurred earlier than the second day.
So we hear- here we have date1< date2.
If you remember up here, date1 is December 31st 1999.
Day2 is January 1st 2000.
So I do think date1 should be less than date2.
Right. What about Date3?
Less than or equal to date one?
Well date3 was in 2016.
So I do not think it occurred before a date in 1989 so I'm hoping that one is false.
And then finally, date2 is the same as date3 equal equal.
Well probably not.
All right so let's see what happens here.
Okay. As I just explained we get true false false.
Hopefully that makes sense to you if it doesn't,
run the program. Think about it.
Flip around the comparison operators and
make sure everything behaves the way you'd expect.
Okay so one thing I can do is check if one date is
before another date after another date the same day and so on.
I can also subtract dates.
This can be valuable as well.
If I wanna find out what is the difference between two dates.
Right. How far apart are they.
Okay. And so you can just use the subtraction operator
say date2-date1 and that does not return a number.
Okay that returns a time delta object.
So I can print that out.
And we'll see what happens.
Right. So day2 it was January 1st 2000 minus date1 which is December 31st 1999.
The difference here is, one day,
zero hours zero point zero seconds.
Okay. That makes sense. All right.
You can actually use a subtraction operator with times as well
so it doesn't just tell you how many days it tells you how many days.
And you know hours minutes and seconds.
If I just want the day so I just want a number.
Right. These timed out objects have these attributes of days.
And so you can just access that directly diff.days.
Right. So once` I have a timed out object,
that days will give me the number of
days and you can see here it printed out one when I printed that out.
Okay. So I can subtract other dates.
Right. date3 minus day2.
So date in 2016 minus you know,
The first day in 2000.
Well there were 5949 days between them.
Okay again I can just get the days and not the whole time that up.
Python's daytime module is actually quite powerful with lots of
features and operations that you can perform on both dates and times.
In this video we've really just scratched the surface.
I've shown you how to create dates,
compare dates, and subtract dates.
But even with these basic operations we can still do quite a lot.
So we're gonna start out by just using these operations to work with dates.