WEBVTT

1
00:00:00.440 --> 00:00:01.800
<v Jose>Hi guys and welcome back.</v>

2
00:00:01.800 --> 00:00:03.380
In this video, I wanted to tell you about

3
00:00:03.380 --> 00:00:06.080
the datetime module, which is a built-in module,

4
00:00:06.080 --> 00:00:08.190
part of the Python standard library

5
00:00:08.190 --> 00:00:10.550
that we can use to deal with dates and times

6
00:00:10.550 --> 00:00:12.063
in our Python code.

7
00:00:13.010 --> 00:00:14.883
What is a datetime object?

8
00:00:14.883 --> 00:00:18.240
This is really the main part of the datetime module.

9
00:00:18.240 --> 00:00:20.560
It allows us to store information

10
00:00:20.560 --> 00:00:22.140
about a certain point in time.

11
00:00:22.140 --> 00:00:23.820
Here in the code block on the left

12
00:00:23.820 --> 00:00:25.570
I've imported the daytime module,

13
00:00:25.570 --> 00:00:27.830
and then created a datetime object

14
00:00:27.830 --> 00:00:29.620
which lives inside the datetime module.

15
00:00:29.620 --> 00:00:32.290
So you're gonna see this datetime.datetime stuff

16
00:00:32.290 --> 00:00:34.630
quite often when your working with Python code,

17
00:00:34.630 --> 00:00:36.820
and then inside it you can pass in

18
00:00:36.820 --> 00:00:38.960
your date and time information

19
00:00:38.960 --> 00:00:41.240
with those keyword arguments.

20
00:00:41.240 --> 00:00:43.240
When you print it out, by default,

21
00:00:43.240 --> 00:00:46.980
it prints out the date and time in the ISO format,

22
00:00:46.980 --> 00:00:49.450
but we can look at how you can change that

23
00:00:49.450 --> 00:00:50.830
in just a moment.

24
00:00:50.830 --> 00:00:53.330
You can also compare dates and times

25
00:00:53.330 --> 00:00:56.340
to see which one is greater than another one for example.

26
00:00:56.340 --> 00:00:59.560
With just the usual mathematical operators.

27
00:00:59.560 --> 00:01:02.000
So you can use greater than, less than, and so on.

28
00:01:02.000 --> 00:01:04.860
And we're gonna leave a resource in the resources section

29
00:01:04.860 --> 00:01:06.530
of this lecture with some more information

30
00:01:06.530 --> 00:01:09.850
of what you can do with these datetime objects.

31
00:01:09.850 --> 00:01:12.120
One of the most common things we do with datetime objects

32
00:01:12.120 --> 00:01:15.640
is get the time right now, and this is how you do it.

33
00:01:15.640 --> 00:01:18.160
You use datetime.datetime.now,

34
00:01:18.160 --> 00:01:20.780
and that is gonna give you the current date and time

35
00:01:20.780 --> 00:01:23.740
up to the microsecond, so it's pretty accurate.

36
00:01:23.740 --> 00:01:26.310
This uses your system's local time

37
00:01:26.310 --> 00:01:27.240
to give you the current time,

38
00:01:27.240 --> 00:01:29.983
so it is gonna show you the time that your computer has.

39
00:01:31.690 --> 00:01:34.318
You can also do datetime.datetime.today

40
00:01:34.318 --> 00:01:36.310
to do essentially the same thing,

41
00:01:36.310 --> 00:01:38.570
but it's generally a less precise alternative

42
00:01:38.570 --> 00:01:41.890
because it doesn't have the same support for timezones,

43
00:01:41.890 --> 00:01:43.490
and we're gonna get into what that is

44
00:01:43.490 --> 00:01:44.740
in a couple of videos time,

45
00:01:44.740 --> 00:01:49.150
but I generally recommend using datetime.datetime.now.

46
00:01:49.150 --> 00:01:51.120
Let's take a look at how to display dates

47
00:01:51.120 --> 00:01:54.570
in a different format other than ISO, and how to parse dates

48
00:01:54.570 --> 00:01:57.060
or how to get a string representing a date

49
00:01:57.060 --> 00:01:59.550
and build a datetime object out of them.

50
00:01:59.550 --> 00:02:01.580
Here's how you can display dates.

51
00:02:01.580 --> 00:02:03.650
What we do is we create a date, for example,

52
00:02:03.650 --> 00:02:05.800
datetime.datetime.now,

53
00:02:05.800 --> 00:02:08.650
and then we do the datetime objects method

54
00:02:08.650 --> 00:02:11.520
and then we grab the datetime object, such as today,

55
00:02:11.520 --> 00:02:16.040
and we do strftime, that's string format time.

56
00:02:16.040 --> 00:02:18.010
That's a method in the datetime object,

57
00:02:18.010 --> 00:02:21.130
and what we pass in is a string that uses

58
00:02:21.130 --> 00:02:23.290
the string formatting mini-language.

59
00:02:23.290 --> 00:02:25.027
For example, for the first one there,

60
00:02:25.027 --> 00:02:29.500
%Y means the year has four digits.

61
00:02:29.500 --> 00:02:32.513
And %m means the month has two digits.

62
00:02:32.513 --> 00:02:35.560
%d means the day has two digits.

63
00:02:35.560 --> 00:02:39.410
So that prints out "2019-12-23," for example.

64
00:02:39.410 --> 00:02:41.020
We've got a complete reference for

65
00:02:41.020 --> 00:02:44.910
the string format mini-language over at strftime.org.

66
00:02:44.910 --> 00:02:46.880
We've not written that, but it's really good.

67
00:02:46.880 --> 00:02:48.730
It has everything that you might possible need

68
00:02:48.730 --> 00:02:51.410
when working with these strings.

69
00:02:51.410 --> 00:02:53.830
In order to parse dates, for example,

70
00:02:53.830 --> 00:02:57.100
here we're asking the user for the current date,

71
00:02:57.100 --> 00:02:59.420
and we can assume they entered the day,

72
00:02:59.420 --> 00:03:01.300
then the month, then the year,

73
00:03:01.300 --> 00:03:05.590
you can then use the datetime class strptime

74
00:03:05.590 --> 00:03:08.050
for string parse time,

75
00:03:08.050 --> 00:03:11.470
to grab the string in the first argument

76
00:03:11.470 --> 00:03:15.490
and pass in the formatting string as the second argument

77
00:03:15.490 --> 00:03:17.630
and then the datetime object is going to be

78
00:03:17.630 --> 00:03:20.940
created for you using that information.

79
00:03:20.940 --> 00:03:24.530
Note that because we've got day, month, and year information

80
00:03:24.530 --> 00:03:26.840
in this string, when we parse it using

81
00:03:26.840 --> 00:03:31.340
%d, %m, %Y, what we end up getting is

82
00:03:31.340 --> 00:03:35.280
that datetime object, but without any time information,

83
00:03:35.280 --> 00:03:37.160
so that just defaults to midnight.

84
00:03:37.160 --> 00:03:39.860
If you wanted to put in hours and minutes and so forth

85
00:03:39.860 --> 00:03:42.660
you could use %h, for example, %m,

86
00:03:42.660 --> 00:03:44.300
and that would give you that.

87
00:03:44.300 --> 00:03:47.550
Again, more information at strftime.org.

88
00:03:47.550 --> 00:03:49.760
Let's take a look at what a timestamp is,

89
00:03:49.760 --> 00:03:52.270
and that is the number of seconds that have passed

90
00:03:52.270 --> 00:03:56.070
since the first of January 1970 at midnight.

91
00:03:56.070 --> 00:03:58.230
Why do we use the number of seconds?

92
00:03:58.230 --> 00:04:00.210
Well, simply because it's easier to work with.

93
00:04:00.210 --> 00:04:01.830
It's easier to work with a single number

94
00:04:01.830 --> 00:04:03.680
rather than many parts of the date like

95
00:04:03.680 --> 00:04:06.250
year, day, month, hour, et cetera.

96
00:04:06.250 --> 00:04:09.370
Datetime objects really have a method called timestamp

97
00:04:09.370 --> 00:04:12.280
that allows us to convert the datetime object

98
00:04:12.280 --> 00:04:14.810
that we are using over to a timestamp.

99
00:04:14.810 --> 00:04:16.560
So you can create the datetime object,

100
00:04:16.560 --> 00:04:19.160
for example with .now, and then convert

101
00:04:19.160 --> 00:04:21.240
to a timestamp with .timestamp.

102
00:04:21.240 --> 00:04:23.210
That is gonna give you a float

103
00:04:23.210 --> 00:04:25.490
that has the decimal place there

104
00:04:25.490 --> 00:04:27.090
for the number of microseconds

105
00:04:27.090 --> 00:04:30.130
because everything before the decimal place is just seconds.

106
00:04:30.130 --> 00:04:32.610
That's just some extra precision there

107
00:04:32.610 --> 00:04:33.900
that often we don't use,

108
00:04:33.900 --> 00:04:35.383
but you can use if you want.

109
00:04:36.470 --> 00:04:38.050
Thank you guys for joining me in this video.

110
00:04:38.050 --> 00:04:39.200
I hope you've learned something.

111
00:04:39.200 --> 00:04:40.723
I'll see you in the next one.

