**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

`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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

So we just got a three there.
1:07

You notice we had the zero before.
1:08

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

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

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

.
1:26

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

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

Just the days changed.
1:34

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

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

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

I did minus five days.
1:47

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

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

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

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

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

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

So it works.
2:11

It gives us the same day.
2:12

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

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

Or we just want a time?
2:20

I just want the date.
2:22

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

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

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

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

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

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

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

So it's one hour long.
2:51

3600 minute seconds.
2:52

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

Cuz you got a lunch hour.
2:59

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

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

hour equals 9, minute equals 0.
3:12

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

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

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

If we did that and
3:32

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

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

So six, 6 p.m.
3:43

That's really nice.
3:44

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

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

So let's see.
3:52

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

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

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

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

Okay?
4:17

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

And if we look at end,
4:23

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

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

library.
4:34

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

a lot of real world scenarios.
4:37

You need to sign up for Treehouse in order to download course files.

Sign up