1
00:00:00,390 --> 00:00:04,410
In this lesson, I want to talk to you about the Python datetime module.

2
00:00:04,920 --> 00:00:09,920
This is a really useful module that helps us work with dates and time.

3
00:00:11,400 --> 00:00:14,550
Going back to our day 32 start project,

4
00:00:14,640 --> 00:00:16,950
I'm going to comment out everything that's there.

5
00:00:18,360 --> 00:00:23,310
And then I'm going to import our datetime module. Now, again,

6
00:00:23,370 --> 00:00:26,010
this is the module that comes preloaded with Python

7
00:00:26,070 --> 00:00:28,800
so you don't have to install it. Now,

8
00:00:28,800 --> 00:00:33,800
one of the most useful things to get with daytime is the current date and time.

9
00:00:35,820 --> 00:00:40,820
And you can write Python code to get that from the computer. Inside this date

10
00:00:40,950 --> 00:00:41,783
time module,

11
00:00:41,790 --> 00:00:46,770
there is a class called datetime as denoted by the c here.

12
00:00:47,610 --> 00:00:51,870
And this is how you get to that class. So to get to that class,

13
00:00:51,900 --> 00:00:54,540
you tap into the datetime module

14
00:00:55,050 --> 00:00:57,900
and then you get to the datetime class.

15
00:00:58,380 --> 00:01:00,210
Now this is really confusing.

16
00:01:00,240 --> 00:01:05,239
So I prefer to rename the datetime module and give it a as so we can shorten it

17
00:01:07,020 --> 00:01:08,370
to just dt.

18
00:01:09,180 --> 00:01:12,300
And that way we can say dt.datetime

19
00:01:12,480 --> 00:01:16,920
and that looks a little bit more easier to understand than before. Nnow,

20
00:01:16,950 --> 00:01:21,330
inside this datetime class, there is a method called now,

21
00:01:21,960 --> 00:01:25,590
and this gets you the current date and time.

22
00:01:26,070 --> 00:01:30,810
So if we save this into a variable called now, and we go ahead and print

23
00:01:30,840 --> 00:01:33,270
now, then when I run this code,

24
00:01:33,300 --> 00:01:38,070
you can see it prints a very long string showing you the current year, month,

25
00:01:38,100 --> 00:01:38,850
day,

26
00:01:38,850 --> 00:01:43,850
as well as time in my local time zone with a high degree of accuracy

27
00:01:44,520 --> 00:01:45,353
here. 

28
00:01:46,880 --> 00:01:49,700
Working with that particular string is not very easy.

29
00:01:49,730 --> 00:01:54,730
If you wanted to check something like if now is the year 2020,

30
00:01:56,810 --> 00:02:00,110
then you can't really do that with just a string like that.

31
00:02:00,740 --> 00:02:03,530
That's why once you've gotten the

32
00:02:03,590 --> 00:02:08,590
now object out of this method because this method of course returns and it gets

33
00:02:09,770 --> 00:02:13,760
saved inside here, then we can tap into some of its attributes.

34
00:02:13,940 --> 00:02:17,330
So for example, we can tap into the year.

35
00:02:18,440 --> 00:02:22,610
So let's save that has the year. And if I print that, you can see

36
00:02:22,610 --> 00:02:26,000
it just gives us the year as a number.

37
00:02:26,780 --> 00:02:29,210
And if I do a type check on that,

38
00:02:29,750 --> 00:02:32,720
you can see that it has a class of integer

39
00:02:33,350 --> 00:02:36,020
whereas if I do a type check on now

40
00:02:36,230 --> 00:02:39,350
it has a class of a datetime object.

41
00:02:40,580 --> 00:02:45,290
So this way, if we managed to get hold of the year as a number,

42
00:02:45,320 --> 00:02:46,970
then we can say something like, well,

43
00:02:46,970 --> 00:02:50,960
if the current year is equal to 2020,

44
00:02:51,350 --> 00:02:53,630
then we will print something like,

45
00:02:55,670 --> 00:03:00,310
and because this rings out as true, then that gets printed out.

46
00:03:01,180 --> 00:03:06,010
In addition to the year, we've also got month, day,

47
00:03:06,190 --> 00:03:10,930
hour, minutes. So you can basically tap into anything in that date

48
00:03:10,930 --> 00:03:14,800
time string as you need. So when you write 'now.'

49
00:03:14,800 --> 00:03:19,690
you can see that there's the year, day, month, hour, minutes,

50
00:03:19,720 --> 00:03:22,000
microsecond, second,

51
00:03:22,450 --> 00:03:26,530
and you can get to the specific part of that datetime that you need.

52
00:03:27,160 --> 00:03:31,510
You can even call other methods like weekday. For example,

53
00:03:33,220 --> 00:03:37,870
if we want to know which day of the week it is, so Monday, Tuesday, Wednesday,

54
00:03:38,170 --> 00:03:42,460
then we can simply tap into now and call this weekday method.

55
00:03:43,270 --> 00:03:46,390
And now if we print out this day of the week,

56
00:03:46,420 --> 00:03:49,210
you can see that it gives us a number.

57
00:03:49,600 --> 00:03:54,310
So it remember that computers start counting from zero so 1

58
00:03:54,340 --> 00:03:57,850
means that its actually the second day of the week, which is Tuesday

59
00:03:57,880 --> 00:03:58,930
as you can see up here.

60
00:04:00,280 --> 00:04:04,540
So we've seen how we can get hold of the current day, year,

61
00:04:04,540 --> 00:04:08,680
month and whichever property you're interested in for today.

62
00:04:09,040 --> 00:04:14,040
But what if we wanted to create a daytime object of our own setting it to a

63
00:04:14,800 --> 00:04:17,740
particular date of our choosing? Well,

64
00:04:17,769 --> 00:04:22,770
let's say that I wanted to create a object that stored my date of birth.

65
00:04:25,390 --> 00:04:30,390
Well then I would tap into the dt module and create a new datetime object

66
00:04:31,300 --> 00:04:35,080
from that class. Now I get to specify, well,

67
00:04:35,080 --> 00:04:38,770
what is the year? What is the month?

68
00:04:39,250 --> 00:04:41,200
What is the day?

69
00:04:41,830 --> 00:04:46,830
And I can even go even more specific like which hour was I born in?

70
00:04:48,190 --> 00:04:52,750
And you'll notice that in here, when you look at the parameters,

71
00:04:53,140 --> 00:04:55,300
the year requires an integer,

72
00:04:55,360 --> 00:04:58,390
the month requires an interger and the day requires an integer.

73
00:04:58,780 --> 00:05:03,730
But after the hour we've got this ... as does the minute and the second

74
00:05:04,060 --> 00:05:07,960
and this is because they have default values. So if I delete this,

75
00:05:09,790 --> 00:05:14,790
the only things I'm required to give when I'm creating a new datetime object is

76
00:05:15,490 --> 00:05:16,570
the year, month and day.

77
00:05:17,260 --> 00:05:22,260
Let's say that I was born in 1995 and I was born in December the 15th.

78
00:05:26,880 --> 00:05:27,713
All right.

79
00:05:30,300 --> 00:05:30,840
Now,

80
00:05:30,840 --> 00:05:35,840
if I print this date of birth object you can see that we get 1995,

81
00:05:38,190 --> 00:05:39,690
12, 15,

82
00:05:40,080 --> 00:05:45,080
and the time in hour, minute and second are set to the default values of zero.

83
00:05:46,890 --> 00:05:51,030
If I wanted to be more specific and I wanted to set my hour of birth,

84
00:05:51,060 --> 00:05:53,520
let's say I was born at 4:00 AM,

85
00:05:53,970 --> 00:05:58,700
then we can set the hour and that updates that default value.

86
00:05:59,450 --> 00:06:04,450
So now we've seen how we can tap into the current date time and also create any

87
00:06:05,210 --> 00:06:07,150
datetime object from scratch

88
00:06:07,510 --> 00:06:12,310
then it's time to put this into practice and use it in our project.

