WEBVTT

1
00:00:00.360 --> 00:00:01.790
<v Jose>Hi guys, and welcome back.</v>

2
00:00:01.790 --> 00:00:03.500
In this video, we're going to talk about

3
00:00:03.500 --> 00:00:07.970
how to do Date arithmetic with the timedelta class.

4
00:00:07.970 --> 00:00:10.230
Something that we see quite often is people trying

5
00:00:10.230 --> 00:00:11.980
to add dates together.

6
00:00:11.980 --> 00:00:13.300
Something like this.

7
00:00:13.300 --> 00:00:16.620
Datetime.datetime.now plus now again,

8
00:00:16.620 --> 00:00:18.310
is always gonna result in an error,

9
00:00:18.310 --> 00:00:20.750
because you can't really add two dates.

10
00:00:20.750 --> 00:00:22.160
What should the output be?

11
00:00:22.160 --> 00:00:24.820
For example, right now it's the year 2020.

12
00:00:24.820 --> 00:00:26.970
Right, if you add today to today,

13
00:00:26.970 --> 00:00:29.130
should we end up in the year 4040.

14
00:00:29.130 --> 00:00:31.820
So clearly, that doesn't make much sense.

15
00:00:31.820 --> 00:00:35.000
But what does make sense is trying to add

16
00:00:35.000 --> 00:00:38.480
a certain number of days, for example, to the current date,

17
00:00:38.480 --> 00:00:41.340
and to do that we use the timedelta class.

18
00:00:41.340 --> 00:00:43.610
So here I've created the current date

19
00:00:43.610 --> 00:00:45.950
using datetime.datetime.now,

20
00:00:45.950 --> 00:00:49.560
and then I've created a seven day difference object

21
00:00:49.560 --> 00:00:53.940
that is a datetime.timedelta at days equals seven.

22
00:00:53.940 --> 00:00:57.080
Now we can add that to a datetime object

23
00:00:57.080 --> 00:01:00.490
and it will increase the date it represents by seven days

24
00:01:00.490 --> 00:01:02.600
without affecting the time in this case,

25
00:01:02.600 --> 00:01:05.490
but we could also subtract it from the current date.

26
00:01:05.490 --> 00:01:07.510
And then we would go back by seven days

27
00:01:07.510 --> 00:01:11.210
timedelta also accepts other arguments as well as days.

28
00:01:11.210 --> 00:01:13.010
We've got seconds, microseconds,

29
00:01:13.010 --> 00:01:16.100
milliseconds, minutes, hours, and weeks.

30
00:01:16.100 --> 00:01:18.520
Some of these get converted to other units,

31
00:01:18.520 --> 00:01:21.210
the timedelta object only stores three of these,

32
00:01:21.210 --> 00:01:22.470
but don't worry about that.

33
00:01:22.470 --> 00:01:25.430
Essentially, it'll handle anything you throw at it

34
00:01:25.430 --> 00:01:27.460
within these arguments.

35
00:01:27.460 --> 00:01:31.055
Another common issue is to try to compare datetimes

36
00:01:31.055 --> 00:01:33.110
and timedelta objects.

37
00:01:33.110 --> 00:01:36.060
On the left we've got something that doesn't really work,

38
00:01:36.060 --> 00:01:37.460
we've got the current date,

39
00:01:37.460 --> 00:01:39.720
and then we've got a one week differential.

40
00:01:39.720 --> 00:01:42.800
If you try to compare one is greater than the other.

41
00:01:42.800 --> 00:01:44.120
That's not gonna make much sense

42
00:01:44.120 --> 00:01:48.130
because one of them represents a current point in time.

43
00:01:48.130 --> 00:01:50.660
The other one represents a difference in time,

44
00:01:50.660 --> 00:01:52.820
it's comparing apples to oranges.

45
00:01:52.820 --> 00:01:55.670
What does work though, is the example on the right,

46
00:01:55.670 --> 00:01:57.160
we've got the current date.

47
00:01:57.160 --> 00:01:58.810
Then we've got the date next week,

48
00:01:58.810 --> 00:02:00.930
which we've calculated by adding seven days

49
00:02:00.930 --> 00:02:02.120
to the current date,

50
00:02:02.120 --> 00:02:04.420
and you can check whether one is greater than the other,

51
00:02:04.420 --> 00:02:05.650
that's totally fine.

52
00:02:05.650 --> 00:02:08.230
So what I'm getting at is when you add a datetime

53
00:02:08.230 --> 00:02:11.760
to a timedelta, you end up with a datetime.

54
00:02:11.760 --> 00:02:13.810
Note that it is clearly not that useful

55
00:02:13.810 --> 00:02:16.630
today is always gonna be smaller than next week

56
00:02:16.630 --> 00:02:18.350
in terms of dates and times.

57
00:02:18.350 --> 00:02:20.840
But what does make sense is for us to try to do

58
00:02:20.840 --> 00:02:22.290
something like this.

59
00:02:22.290 --> 00:02:24.910
Here, we're asking the user for a date.

60
00:02:24.910 --> 00:02:27.280
Then we're parsing it using strptime

61
00:02:27.280 --> 00:02:29.320
as we learned in the last video,

62
00:02:29.320 --> 00:02:33.030
and then we are calculating whether the date

63
00:02:33.030 --> 00:02:35.960
that they entered is greater than next week.

64
00:02:35.960 --> 00:02:38.143
So here we've got their datetime.

65
00:02:39.150 --> 00:02:40.540
Here we've got next week,

66
00:02:40.540 --> 00:02:43.410
we should calculate by adding seven days to today.

67
00:02:43.410 --> 00:02:45.010
And then we compare them

68
00:02:45.010 --> 00:02:46.510
and if they're not painting within a week,

69
00:02:46.510 --> 00:02:48.060
then we call them a slacker.

70
00:02:48.060 --> 00:02:50.030
I should run this programme for myself as well.

71
00:02:50.030 --> 00:02:52.010
I need to paint my shed.

72
00:02:52.010 --> 00:02:54.180
So yeah, that's everything that we've got here

73
00:02:54.180 --> 00:02:57.980
for comparing datetimes and timedeltas and using timedeltas

74
00:02:57.980 --> 00:03:00.210
to perform datetime arithmetic.

75
00:03:00.210 --> 00:03:01.400
I hope you guys have learned something.

76
00:03:01.400 --> 00:03:04.350
Thank you for watching, and I'll see you in the next video.

