#100DaysOfCode in Python Transcripts
Chapter: Days 1-3: Playing with Datetimes
Lecture: Learning datetime and date
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Given datetime is part of the Python standard lib,
0:04
we don't actually have to do any setup here.
0:07
You'll see in the coming videos
0:09
that you will have to do setup steps,
0:12
create virtual environments and whatnot,
0:14
but given this is datetime, we don't really have to.
0:17
And, I think it'd be best for us to just work
0:20
in the Python shell here.
0:21
This is IDLE, the default Python IDE
0:25
that it ships with.
0:27
So, let's have a play with that.
0:29
Now, the first thing we're going to do
0:31
is we're going to import datetime.
0:34
But, we're actually going to do
0:37
from datetime import datetime, okay?
0:44
And, this is just going to make it a bit easier for us
0:46
when we're typing in the rest of our code.
0:48
And, just to get yourself prepared, let's just
0:52
from datetime import date
0:54
that's for a bit later in this video.
0:58
Alright so what is datetime, alright.
1:00
For those who are unaccustomed and unaware
1:03
datetime is just the Python library module that allows
1:07
you to deal with dates and times.
1:11
Pretty self-explanatory, right?
1:14
So, if you want to deal with just the dates
1:18
so, you know, today's date, let's call it
1:21
the 23rd of February 2018, not very specific.
1:26
Or if you want to deal with the time
1:29
that you've got to think about that
1:30
from a programming perspective, there is a difference, okay.
1:35
So, datetime allows us to deal with
1:38
the entire time set, the entire timeframe.
1:41
You're talking seconds, minutes, hours, days
1:44
all the way through to years, okay?
1:47
We can visualize that with datetime.today().
1:52
If we hit enter, there we go, we can see today's date.
1:57
The 24th of February 2018
2:01
but we also get this timestamp.
2:03
It's 10:17pm
2:05
and these are the extra seconds here.
2:08
So seconds, milliseconds and whatnot, okay?
2:12
Now I'm going to show you this, what kind of an object is this?
2:16
Well let's go, well first actually we have to assign that
2:20
to something that way so, we'll just go with today.
2:24
Here's datetime.today()
2:28
alright and then we'll type it out, so type today.
2:32
So it's a datetime object, okay?
2:35
And that's important because you can't mix these objects.
2:39
I'll point that out in just a minute.
2:42
So with this timestamp, there is more you can do with that.
2:46
And I'll show you that in the next video with timedelta.
2:51
Alright, but for now just understand that this is what your
2:53
standard datetime format will look like.
2:56
This is the sort of data you're going to get back.
2:59
And this is really useful for times when you want to
3:03
deal with say, subscriptions or anything like that
3:06
where it has to do with exact timestamps, or logging
3:10
or anything where you need to know
3:13
the time that something happened.
3:15
Going by the date of say, the 24th of February
3:19
is not accurate enough, okay, there is 24 hours
3:22
within that day so, a lot of things could have happened.
3:25
Alright, so we'll move on to the date part here.
3:30
So we'll just go today date, we'll create that variable.
3:34
Here's date.today(), so you can see straightaway
3:38
we're not using datetime, we're using the date section
3:43
okay, we're using the date option here.
3:45
So date.today()
3:48
and if we type that out
3:53
Today date, we can see
3:56
the different type of object here.
3:58
First one was a datetime and now it's a date object, okay?
4:04
And we can see what that looks like with today date.
4:09
And we have just the date string, okay?
4:12
So we don't have the extra time on the end.
4:15
And this is, again, very useful.
4:18
So you can see the distinction between the two of them.
4:21
Alright let's get ourselves a little bit of white space.
4:25
Now one really cool thing that I love about date
4:30
is that we can drill into it a little more, so we can go
4:36
today.month is 2.
4:41
So you can see we can actually tear it apart a bit.
4:44
So today.day
4:47
is 24 and then
4:51
today.year, and we get 2018.
4:57
So now you can sort of visualize how date can help you
4:59
in your projects, right, if you're not already using it.
5:03
It's actually really cool.
5:04
So one really, really cool thing that
5:07
has come in handy over time,
5:10
is the fact that you can do
5:11
a bit of math with your dates, alright.
5:15
So we'll go, let's just go something easy.
5:18
So Christmas, what's the date for Christmas?
5:21
It's the, we'll go year first, so 2018.
5:25
It's the month next, so 12.
5:29
And then it's the day, so 25th, alright.
5:35
Now one thing, if you had a look, this is ...
5:40
us specifying a date, this is us
5:42
assigning a date to a variable.
5:45
So now the Christmas variable is always going to
5:50
have this date assigned to it.
5:54
You can see that there, okay.
5:57
Now, this is really cool, so
6:00
We can actually go Christmas, cause we know that's
6:04
the end of this year, minus, today date.
6:10
Kay, and that's 304 days, it automatically called
6:15
on timedelta, so that's giving away something for the next
6:18
video but, carry on, 304 days.
6:22
Alright, and we can see that visualized a different way.
6:25
We can, and this is again giving more away
6:27
we can go Christmas
6:31
minus today
6:35
in days, so .days.
6:39
304 days, alright and this is really cool for something
6:43
such as this, I'm just going to copy and paste here
6:46
rather than type it all out for you, alright.
6:50
So if Christmas is not today date
6:55
well what can we do?
6:56
We can print a certain message.
6:58
Again, you can see this is useful for certain other projects
7:03
so print, sorry there are still this many days
7:09
(christmas minus today).days, until Christmas.
7:13
Okay, and then else ...
7:16
We'll copy and paste this as well.
7:20
We're going to print some sort of message, alright.
7:25
"Yay, it's Christmas."
7:27
So, by hitting enter, sorry there are still 304
7:32
the same value here, until Christmas.
7:35
I've obviously left out the word 'days'
7:37
so that's my mistake, but
7:38
sorry there are still 304 days until Christmas.
7:42
If I happen to wait another, you know, ha ha ha ha
7:46
that many days, 304 days
7:48
we would then get this message here.
7:51
So this is date and this is datetime.
7:54
Very, very tedious at times, I want to say
7:59
but so useful, so this is a great place to start
8:02
manipulating your code, manipulate your dates
8:06
and have some fun with it.
8:08
And in the next video we're going to look at datetime.