Jose: Hi guys, and welcome back.
In this video, we're going to talk about
how to do Date arithmetic with the timedelta class.
Something that we see quite often is people trying
to add dates together.
Something like this.
Datetime.datetime.now plus now again,
is always gonna result in an error,
because you can't really add two dates.
What should the output be?
For example, right now it's the year 2020.
Right, if you add today to today,
should we end up in the year 4040.
So clearly, that doesn't make much sense.
But what does make sense is trying to add
a certain number of days, for example, to the current date,
and to do that we use the timedelta class.
So here I've created the current date
using datetime.datetime.now,
and then I've created a seven day difference object
that is a datetime.timedelta at days equals seven.
Now we can add that to a datetime object
and it will increase the date it represents by seven days
without affecting the time in this case,
but we could also subtract it from the current date.
And then we would go back by seven days
timedelta also accepts other arguments as well as days.
We've got seconds, microseconds,
milliseconds, minutes, hours, and weeks.
Some of these get converted to other units,
the timedelta object only stores three of these,
but don't worry about that.
Essentially, it'll handle anything you throw at it
within these arguments.
Another common issue is to try to compare datetimes
and timedelta objects.
On the left we've got something that doesn't really work,
we've got the current date,
and then we've got a one week differential.
If you try to compare one is greater than the other.
That's not gonna make much sense
because one of them represents a current point in time.
The other one represents a difference in time,
it's comparing apples to oranges.
What does work though, is the example on the right,
we've got the current date.
Then we've got the date next week,
which we've calculated by adding seven days
to the current date,
and you can check whether one is greater than the other,
that's totally fine.
So what I'm getting at is when you add a datetime
to a timedelta, you end up with a datetime.
Note that it is clearly not that useful
today is always gonna be smaller than next week
in terms of dates and times.
But what does make sense is for us to try to do
something like this.
Here, we're asking the user for a date.
Then we're parsing it using strptime
as we learned in the last video,
and then we are calculating whether the date
that they entered is greater than next week.
So here we've got their datetime.
Here we've got next week,
we should calculate by adding seven days to today.
And then we compare them
and if they're not painting within a week,
then we call them a slacker.
I should run this programme for myself as well.
I need to paint my shed.
So yeah, that's everything that we've got here
for comparing datetimes and timedeltas and using timedeltas
to perform datetime arithmetic.
I hope you guys have learned something.
Thank you for watching, and I'll see you in the next video.