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
`timedelta` is a really handy tool. Let's see more about how to use them.
timedelta
objects represent gaps in time. They are returned when you subtract one datetime
from another. They can also be assigned to a variable and then used to augment datetime
objects.

0:00
Like we saw in the last video, timedelta objects represent a gap of time.

0:04
We can get them by subtracting one datetime object from another, but

0:08
we can actually create them on their own.

0:10
Let's check them out on their own with datetimes, with dates, and with times.

0:15
So since I've reset my Python interpreter, I need to import datetime again.

0:21
And let's make an object that represents now, just so

0:24
we don't have to keep typing out datetime.datetime.now.

0:28
And we don't really care if we have new values or not.

0:31
We just want something that's held on to here.

0:34
Let's see if we can move this forward in time by three days.

0:38
So, the way that we manipulate date times is with our timedeltas.

0:43
So, let's make a timedelta and we can say in here like, hours equals 5.

0:49
Okay, and I've got this thing back and you see it's 18,000 seconds.

0:54
What I wanna do though is I want to make one of these that's days.

0:59
So, I wanna do days equals we said 3, so 3.

1:07
So we just got a three there.

1:08
You notice we had the zero before.

1:10
That's because it was zero days it was just five hours.

1:13
Okay if I was to do now plus daytime.time delta days equals 3.

1:19
Then where it was the 15th, it is now the 18th, okay?

1:26
.

1:26
Everything else is exactly the same, right?

1:29
It's still 18 hours and 30 seconds or sorry, 18 hours and 30 minutes.

1:34
Just the days changed.

1:35
All right, what if I wanted to go back in time?

1:38
So we could do now plus datetime.timedelta,

1:44
days equals, lets go back five days.

1:47
I did minus five days.

1:49
So again, we went from the 15th to the 10th.

1:52
Now, adding negative five days, that's a little confusing to some people.

1:57
I can see why, it's a little strange.

1:59
So what we can do is we can actually subtract.

2:01
So, let's do now minus datetime.timedelta days equal 5.

2:07
So we've subtracted five days, we get the exact same number.

2:11
So it works.

2:12
It gives us the same day.

2:13
So we have two different ways to manipulate our datetimes.

2:17
So what if we just want a date?

2:20
Or we just want a time?

2:22
I just want the date.

2:23
I don't care about what time it is.

2:24
So I can actually do now.date, and I get back a date.

2:29
And I can do now.time and I get back a time.

2:33
So these can be used if I just need the time, I just need the, the date, whatever.

2:38
What else can we do with these, though?

2:40
Well, we can work with multiple timedeltas, which is kinda cool.

2:44
So let's make a timedelta here that's hour, which is a timedelta hours equals 1.

2:51
So it's one hour long.

2:52
3600 minute seconds.

2:55
And we're gonna say a workday equals hour times 9.

2:59
Cuz you got a lunch hour.

3:01
So eight hour day plus one hour lunch gives you nine hours, right?

3:05
Let's do tomorrow equals datetime.datetime.now.replace

3:12
hour equals 9, minute equals 0.

3:16
And then really, we should do plus datetime.timedelta days equal 1.

3:26
So if we look at tomorrow, that's tomorrow morning at 9 a.m, more or

3:30
less we've got a couple seconds in there, but whatever.

3:32
If we did that and

3:33
we want to say okay what time would I be off tomorrow, if it wasn't standard.

3:38
We could do tomorrow plus work day and we get out 18:00.

3:43
So six, 6 p.m.

3:44
That's really nice.

3:46
I, I can use this to calculate the ends of an appointment or something.

3:49
I just need to make a timedelta as the length of the appointment.

3:52
So let's see.

3:53
We did appointment equals datetime.timedelta minutes equals 45.

3:59
Maybe I have a standard 45 minute meeting slot that I give out.

4:03
All right and we'll say the start is a datetime of the 1st of November and

4:12
it's gonna be 45 minutes after noon.

4:17
Okay?

4:18
So, the end of that is going to be start plus appointment.

4:23
And if we look at end,

4:24
then we see that it ends at 1:30 which is 45 minutes after 12:45.

4:30
>> Manipulating datetimes with timedelta is probably the best part of the datetime

4:34
library.

4:35
It's fairly simple and intuitive and it's really handy for

4:37
a lot of real world scenarios.
You need to sign up for Treehouse in order to download course files.
Sign up