Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's look at the `.now()` and `.today()` methods.
We've seen that datetime can make objects
for right now.
0:00
And we can manipulate those with time
deltas.
0:03
But, what if we want it to be for today,
or to combine dates and times together?
0:06
Let's look at all of the available
attributes on the datetime object.
0:11
So just like before, we want to import
datetime.
0:14
And, let's make a couple of variables
here.
0:18
We're gonna call one now, which would be
datetime.datetime.now.
0:22
And let's do another one which is today,
which will be datetime.datetime.today.
0:26
So, let's look at these.
0:32
So it's kind of strange.
0:34
They're almost the exact same thing.
0:35
They both have the same date.
0:37
They both have the same hour.
0:39
They both have the same minute.
0:40
The only difference is the seconds,
0:41
because one ran a few seconds after the
other one.
0:43
So what's up with this?
0:46
Well, .now and .today both do the same
thing.
0:46
The difference is that now can take a
timezone as an argument,
0:50
so that you can control where the now is.
0:54
And today doesn't.
0:59
But we're not quite ready for time zones
yet.
1:00
We're gonna talk about those at the end of
the course.
1:02
So let's make a today that more accurately
reflects what I think of as today,
1:04
which would be today's date with midnight
as the time.
1:11
And we're gonna do that by using the
combine method in the datetime library,
1:16
or datetime class.
1:20
Sorry.
1:22
So, datetime.datetime.combine, and
1:23
what it lets us do is it lets us combine a
date and a time.
1:26
This is probably the only time that you'll
really use date.
1:30
Because data and time just aren't all that
useful on their own.
1:34
And then daytime.time.
1:39
And so, doing an empty time like this
generates one that's at midnight.
1:41
So, it looks like it's a day.
1:45
There's our today.
1:48
So, that's what we want.
1:49
We've got today at midnight.
1:50
All right, that's great.
1:53
What if we just wanna get out some
attributes from this?
1:54
So, if we do like today.month.
1:57
You get ten and today.hour you get zero.
2:00
And today.year.
2:05
And these work on now as well.
2:06
We can do now.hour we get different hours.
2:09
We've actually kind of already played with
these when we were getting the seconds to
2:12
calculate between the two times.
2:15
That was off of a time delta.
2:16
But these work the same way.
2:19
They expose the same api for something
like that, which is very common in python.
2:21
Related classes will work the same way,
because they're related.
2:26
And python doesn't want you to be
surprised while you're working.
2:30
Okay, so let's try something a little bit
more generalized.
2:34
What if I wanna know what day of the week
today is?
2:37
Well, I can call the weekday method, and I
get back two.
2:41
Now, this is coming from a list and
python's weeks start on Monday.
2:46
Monday is zero, so that means Tuesday is
one, and number two is Wednesday.
2:50
Okay, last thing to talk about here,
2:56
what if I want the POSIX timestamp for a
certain date and time?
2:58
So basically, what we have is January 1st,
1970 is when the epoch started.
3:03
And we've kicked up one second for every
second since then.
3:08
So, what if I wanna get now's time stamp?
3:11
The POSIX time stamp for now.
3:16
And there we go.
3:19
We have this huge number and then .31
whatever.
3:20
So the reason it's a float is because it's
always at some point of the second.
3:23
We've, we're not necessarily at the
absolute beginning or the absolute.
3:27
Well, we know we're at the beginning to
get zero of the second.
3:30
We're somewhere in the middle.
3:33
So we are a third of the way through the
second, when we did the now.time stamp.
3:34
Now, this uses your local system time.
3:40
In this case, it uses the server that work
space is running on,
3:43
because we don't have a timezone in here.
3:47
You don't always need the POSIX time
stamp, but
3:48
when you do it's very handy to be able to
get one.
3:50
The attributes on datetime objects are
really handy, and make working with
3:53
datetimes more pleasant, because you can
get to just the data that you want.
3:57
In our next video, we're gonna talk about
how to get that data out in
4:00
even more customizable formats.
4:03
You need to sign up for Treehouse in order to download course files.
Sign up