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
We'll just dive in and start tweaking dates and times to our liking.
Here are the datetime
docs
Examples
>>> import datetime
>>> now = datetime.datetime.now()
>>> morning = now.replace(hour=9, minute=0)
The above will make a variable now
that represents now, and then change the time to 9am in the variable morning
.
>>> datetime.datetime(2014, 10) - datetime.datetime(2014, 9)
The above will give back a datetime.timedelta
object.
[MUSIC]
0:00
All programmers eventually have to deal
with dates and times.
0:04
Sometimes at the same time, and
eventually,
0:08
you're going to have to deal with time
zones too.
0:11
All of this can be a big mess, but
thankfully,
0:13
Python gives us some really solid tools
for dealing with them.
0:15
We'll be using the datetime library for
everything in this course.
0:19
So get used to importing datetime.
0:21
So first things first, we're gonna want to
import datetime.
0:24
So we'll type that out and
0:29
we're gonna be using a class from datetime
that's named datetime.
0:31
Yeah, this can be a little confusing, but
you'd get used to it pretty quickly.
0:36
Basically, datetime has four major modules
that you'll sometimes use, maybe five.
0:40
It's got date, time, and then those
combined become datetime.
0:48
So we have date, we have time, and we have
datetime.
0:53
And then you'll find that we're gonna use
timedelta quite a bit.
0:58
And we may also be using timezone toward
the end.
1:01
You won't really use tzinfo on its own.
1:05
You're not supposed to.
1:08
And then really time and date don't end up
getting used a whole lot.
1:10
You mostly use datetime itself.
1:14
So, let's look at what datetime does.
1:17
It lets us work, as I've just said, with
both times and dates at the same time.
1:19
For right now though, let's just play with
two methods that datetime gives us.
1:24
Sorry, datetime.datetime.
1:28
Yeah, that gets confusing, doesn't it?
1:30
So anyway, those two methods are .now and
.replace.
1:32
So let's try datetime.datetime.now.
1:35
And you can see that it's 2014, it's the
10th month, the 15th day,
1:40
the 18th hour on our server, the 23rd
minute, 0 seconds and
1:46
596,134 microseconds, which is a millionth
of a second.
1:51
Yeah, anyway, something like that.
1:57
All right, that's the current date.
1:59
Well, it's not right now, it's the now
that was right now when I ran that method.
2:01
It's really best if you don't think about
it too much.
2:06
Let's, let's make a new variable.
2:08
We'll call this treehouse_start, and it'll
be datetime.datetime.now.
2:11
So let's look at that.
2:18
That's a similar date to what we'd had a
minute ago.
2:21
But actually, I want to do
treehouse_start.replace.
2:24
And I want to turn the hour into 9, the
minute into 0, and
2:28
the second into 0, and the microsecond
into 0.
2:35
Oh, we wanna do treehouse_start equals
2:40
treehouse_start.replace, hour equals 9,
2:44
minute equals 0, second equals 0,
microsecond equals o.
2:49
All right, so now, if we look at
treehouse_start, we get,
2:55
that I started at 9 AM this morning.
2:59
So what does this replace method that do?
3:02
Well, if the name didn't give it away,
3:06
it lets you replace attributes of our
datetime object.
3:08
So we can replace the hour, the minute,
the second, or the microsecond.
3:12
We can also replace the year, the month,
the day, whatever we need to replace.
3:15
This comes in really handy when you need
to make your dates and times some time
3:21
other than now, but you don't want to deal
with the other ways of creating them.
3:26
Which, lets talk about that.
3:30
We could also have done.
3:31
I'll do this one as th-start,
datetime.datetime, 2014, 10, 15, 9.
3:33
And if I look at th_start,
3:39
and I look at treehouse_start, they're the
same thing.
3:45
But sometimes you don't want to just fill
in those variables,
3:49
you want to I want all these things as
they're gonna come out.
3:52
I want the year, the month, the day.
3:56
I just wanna change the hour to be, in
this case 9 AM,
3:57
or to be midnight or whatever.
4:01
Okay, so lets see how long I've been at
work, according to the server,
4:04
which is a little different from my
computer I'm actually typing on.
4:09
So we'll do datetime.datetime.now.
4:13
And I'm gonna subtract from that,
treehouse_start.
4:16
And, we get back this new thing here
called a timedelta, and it's got some
4:20
numbers that don't necessarily make a lot
of sense, this 33972 and 762540.
4:25
What those actually are, the 0 is days.
4:31
And the other measures there.
4:36
So the 0 is days, the second number is the
number of seconds, and the third
4:38
one here is the number of microseconds,
which are a thousand milliseconds.
4:44
So a millionth of a second.
4:48
So it's been 0 days, 33,972 seconds and
4:51
762,540 microseconds since I started work
according to the server.
4:56
Okay.
5:03
How do we turn that into hours though?
5:03
Let's, let's turn it into a variable.
5:07
We'll do time_worked equals
5:09
datetime.datetime.now minus
treehouse_start.
5:12
Okay?
5:17
And so, time_worked is gonna be very
similar to that
5:18
number that we have before, and let's see
what it has in here.
5:20
Let's do time_worked.days, 0 days,
correct?
5:24
All right, and microseconds.
5:30
There's that 850,000 number.
5:33
And if we just did seconds, okay.
5:36
Let's actually do a dir on time_worked and
see what we get.
5:40
So, we can see that this has days, max,
5:45
microseconds, it's got a lot of stuff
here.
5:47
If we have the seconds though, we can
calculate the hours.
5:50
There are 60 seconds in a minute, 60
minutes in an hour.
5:54
So if we divide our time worked seconds
attribute by 3600, then we have our hours.
5:57
Lets round those so that it might look a
little bit more impressive.
6:04
I don't know.
We'll see.
6:09
So time_worked.seconds divided by 3600.
6:10
Notice no spaces around my divide sign cuz
I'm inside a function call.
6:14
And hours_worked is 9.
6:18
That's not accurate.
6:23
The server is in a different timezone.
6:23
I, I imagine the server is set to UCT
time, so it thinks it's a little further
6:26
than it actually is, but you get the idea
of rounding these things.
6:30
So, awesome.
6:34
It thinks I've been to work for 9 hours.
6:35
Oh, if only.
6:37
Wow, that was a lot for just the first
video, datetime objects,
6:39
timedelta objects, and we're already
manipulating time.
6:42
We can actually do a lot with timedeltas,
so let's explore them a bit more.
6:46
You need to sign up for Treehouse in order to download course files.
Sign up