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