#100DaysOfCode in Python Transcripts
Chapter: Days 1-3: Playing with Datetimes
Lecture: Datetime timedelta usage
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Okay, just like the previous day, we're going to look at something but we're going to use the Python shell for this one.
0:07
And specifically today we're looking at timedelta. So what is timedelta? Well, timedelta is pretty much a gap in time measured out.
0:19
So, for example, if you want to calculate something such as how many hours from now until a certain point in time,
0:26
you can use timedelta for that to specify what it's going to be. A real world example. How many hours until I go to bed?
0:34
Well, let's say it's going to be four hours. So my timedelta, you can specify for this calculation, is four hours.
0:43
And four hours from now could be two in the morning, okay? So it's different... That's how you calculate things like that, you use timedelta.
0:54
All right, so how do we do that? Well, we go from datetime import datetime just like usual, from datetime import timedelta.
1:07
All right, so let's represent our timedelta as a variable t timedelta and let's work on days and hours. So let's say we have four days and 10 hours
1:21
until my next day off work, it's pretty depressing. And how do we deal with this? How do we work with this?
1:29
Well, let's first of all confirm we have a timedelta object there, excellent. And what next? What can we do with this?
1:37
Well, we can go how many days in there. So t.days. That gives us four days, okay? One important thing to note here, watch this next one. T.seconds.
1:51
36,000. So 36,000 seconds is not four days 10 hours. 36,000 seconds is just the 10 hours. And why is that? Well, this timedelta is just like...
2:08
Imagine the stopwatch on your watch, it's only able to go up to a certain amount of time, right? Maybe 23 hours and 59 minutes.
2:16
So with timedelta, the seconds, it's only able to go up to a maximum of one day, okay? So we have four full days here,
2:27
so it's not going to show us the seconds in four full days. It's only going to show us the seconds in the hours.
2:34
So you have to take that into account and your calculation. Okay? We could calculate the hours but not like this. Okay?
2:45
It doesn't allow us to do this because it has seconds, it's not going to bother with hours, all right? So in order to get around this,
2:53
well, you have to do a bit of maths, unfortunately for people like me. So t.seconds divided by 60 and divided by 60 again.
3:04
Well, because we have 60 seconds in a minute and then 60 minutes in an hour. And that gives us that 10 hours.
3:11
Alternatively, you could write that as t.seconds / 3,600. Same thing, okay? That's a really important gotcha because it definitely got me.
3:26
back at the start. So here is an example of a sort of scenario you could use it in, but just keep in mind, timedelta is that gap,
3:36
it's that sort of way of representing the time between two points in time, okay? All right, so we have an ETA. Well, let's just say it's the ETA until
3:50
I wake up. So hours equals six. We're not even going to talk days here, okay? We can go today. We'll give ourselves a datetime today, variable, okay?
4:05
We're not dealing with just date, we're dealing with day time because we want the time, we want the minutes, the seconds, the hours, right?
4:12
So there we go, we've got two variables, ETA and today. All right? So today, let's just show you what that is. It's currently 10:39 p.m., okay?
4:26
Let's get rid of that. All right. We can go what is ETA? Is our timedelta, all right? Now, what next? We want to add these two together, okay?
4:43
So we can go today + ETA, this is the beauty, the absolute beauty of timedelta, we can just add it straight to a datetime object
4:56
which is so cool and so handy and it makes it so easy. So today plus ETA. And look at that time. It actually changed the date to the 25th
5:10
because we'd cross over midnight and it says six hours from now is 4:39 a.m., okay? And this is really, really cool
5:20
because you don't have to worry about any conversions, you don't have to change anything. It's so easy. And even better than that,
5:28
we can format it, so today + ETA as a string. Look at that, it's glorious. We have an actual nicely formatted date string and time stamp.
5:46
How awesome is that? And that's timedelta, that's really the bread and butter of timedelta. You're dealing with just setting yourself a static time,
5:56
a static amount of time and then you can add it, subtract it, do whatever you want with it. And this is really useful in a lot of programs,
6:04
so keep this one in your belt