WEBVTT

0
00:00.390 --> 00:04.410
In this lesson, I want to talk to you about the Python datetime module.

1
00:04.920 --> 00:09.920
This is a really useful module that helps us work with dates and time.

2
00:11.400 --> 00:14.550
Going back to our day 32 start project,

3
00:14.640 --> 00:16.950
I'm going to comment out everything that's there.

4
00:18.360 --> 00:23.310
And then I'm going to import our datetime module. Now, again,

5
00:23.370 --> 00:26.010
this is the module that comes preloaded with Python

6
00:26.070 --> 00:28.800
so you don't have to install it. Now,

7
00:28.800 --> 00:33.800
one of the most useful things to get with daytime is the current date and time.

8
00:35.820 --> 00:40.820
And you can write Python code to get that from the computer. Inside this date

9
00:40.950 --> 00:41.783
time module,

10
00:41.790 --> 00:46.770
there is a class called datetime as denoted by the c here.

11
00:47.610 --> 00:51.870
And this is how you get to that class. So to get to that class,

12
00:51.900 --> 00:54.540
you tap into the datetime module

13
00:55.050 --> 00:57.900
and then you get to the datetime class.

14
00:58.380 --> 01:00.210
Now this is really confusing.

15
01:00.240 --> 01:05.240
So I prefer to rename the datetime module and give it a as so we can shorten it

16
01:07.020 --> 01:08.370
to just dt.

17
01:09.180 --> 01:12.300
And that way we can say dt.datetime

18
01:12.480 --> 01:16.920
and that looks a little bit more easier to understand than before. Nnow,

19
01:16.950 --> 01:21.330
inside this datetime class, there is a method called now,

20
01:21.960 --> 01:25.590
and this gets you the current date and time.

21
01:26.070 --> 01:30.810
So if we save this into a variable called now, and we go ahead and print

22
01:30.840 --> 01:33.270
now, then when I run this code,

23
01:33.300 --> 01:38.070
you can see it prints a very long string showing you the current year, month,

24
01:38.100 --> 01:38.850
day,

25
01:38.850 --> 01:43.850
as well as time in my local time zone with a high degree of accuracy

26
01:44.520 --> 01:45.353
here. 

27
01:46.880 --> 01:49.700
Working with that particular string is not very easy.

28
01:49.730 --> 01:54.730
If you wanted to check something like if now is the year 2020,

29
01:56.810 --> 02:00.110
then you can't really do that with just a string like that.

30
02:00.740 --> 02:03.530
That's why once you've gotten the

31
02:03.590 --> 02:08.590
now object out of this method because this method of course returns and it gets

32
02:09.770 --> 02:13.760
saved inside here, then we can tap into some of its attributes.

33
02:13.940 --> 02:17.330
So for example, we can tap into the year.

34
02:18.440 --> 02:22.610
So let's save that has the year. And if I print that, you can see

35
02:22.610 --> 02:26.000
it just gives us the year as a number.

36
02:26.780 --> 02:29.210
And if I do a type check on that,

37
02:29.750 --> 02:32.720
you can see that it has a class of integer

38
02:33.350 --> 02:36.020
whereas if I do a type check on now

39
02:36.230 --> 02:39.350
it has a class of a datetime object.

40
02:40.580 --> 02:45.290
So this way, if we managed to get hold of the year as a number,

41
02:45.320 --> 02:46.970
then we can say something like, well,

42
02:46.970 --> 02:50.960
if the current year is equal to 2020,

43
02:51.350 --> 02:53.630
then we will print something like,

44
02:55.670 --> 03:00.310
and because this rings out as true, then that gets printed out.

45
03:01.180 --> 03:06.010
In addition to the year, we've also got month, day,

46
03:06.190 --> 03:10.930
hour, minutes. So you can basically tap into anything in that date

47
03:10.930 --> 03:14.800
time string as you need. So when you write 'now.'

48
03:14.800 --> 03:19.690
you can see that there's the year, day, month, hour, minutes,

49
03:19.720 --> 03:22.000
microsecond, second,

50
03:22.450 --> 03:26.530
and you can get to the specific part of that datetime that you need.

51
03:27.160 --> 03:31.510
You can even call other methods like weekday. For example,

52
03:33.220 --> 03:37.870
if we want to know which day of the week it is, so Monday, Tuesday, Wednesday,

53
03:38.170 --> 03:42.460
then we can simply tap into now and call this weekday method.

54
03:43.270 --> 03:46.390
And now if we print out this day of the week,

55
03:46.420 --> 03:49.210
you can see that it gives us a number.

56
03:49.600 --> 03:54.310
So it remember that computers start counting from zero so 1

57
03:54.340 --> 03:57.850
means that its actually the second day of the week, which is Tuesday

58
03:57.880 --> 03:58.930
as you can see up here.

59
04:00.280 --> 04:04.540
So we've seen how we can get hold of the current day, year,

60
04:04.540 --> 04:08.680
month and whichever property you're interested in for today.

61
04:09.040 --> 04:14.040
But what if we wanted to create a daytime object of our own setting it to a

62
04:14.800 --> 04:17.740
particular date of our choosing? Well,

63
04:17.770 --> 04:22.770
let's say that I wanted to create a object that stored my date of birth.

64
04:25.390 --> 04:30.390
Well then I would tap into the dt module and create a new datetime object

65
04:31.300 --> 04:35.080
from that class. Now I get to specify, well,

66
04:35.080 --> 04:38.770
what is the year? What is the month?

67
04:39.250 --> 04:41.200
What is the day?

68
04:41.830 --> 04:46.830
And I can even go even more specific like which hour was I born in?

69
04:48.190 --> 04:52.750
And you'll notice that in here, when you look at the parameters,

70
04:53.140 --> 04:55.300
the year requires an integer,

71
04:55.360 --> 04:58.390
the month requires an interger and the day requires an integer.

72
04:58.780 --> 05:03.730
But after the hour we've got this ... as does the minute and the second

73
05:04.060 --> 05:07.960
and this is because they have default values. So if I delete this,

74
05:09.790 --> 05:14.790
the only things I'm required to give when I'm creating a new datetime object is

75
05:15.490 --> 05:16.570
the year, month and day.

76
05:17.260 --> 05:22.260
Let's say that I was born in 1995 and I was born in December the 15th.

77
05:26.880 --> 05:27.713
<v 1>All right.</v>

78
05:30.300 --> 05:30.840
<v 0>Now,</v>

79
05:30.840 --> 05:35.840
if I print this date of birth object you can see that we get 1995,

80
05:38.190 --> 05:39.690
12, 15,

81
05:40.080 --> 05:45.080
and the time in hour, minute and second are set to the default values of zero.

82
05:46.890 --> 05:51.030
If I wanted to be more specific and I wanted to set my hour of birth,

83
05:51.060 --> 05:53.520
let's say I was born at 4:00 AM,

84
05:53.970 --> 05:58.700
then we can set the hour and that updates that default value.

85
05:59.450 --> 06:04.450
So now we've seen how we can tap into the current date time and also create any

86
06:05.210 --> 06:07.150
datetime object from scratch

87
06:07.510 --> 06:12.310
then it's time to put this into practice and use it in our project.