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