1 00:00:00,500 --> 00:00:04,980 Like we saw in the last video, timedelta objects represent a gap of time. 2 00:00:04,980 --> 00:00:08,310 We can get them by subtracting one datetime object from another, but 3 00:00:08,310 --> 00:00:10,550 we can actually create them on our own. 4 00:00:10,550 --> 00:00:14,350 Let's check them out on their own with datetimes, with dates, and with times. 5 00:00:15,430 --> 00:00:20,550 So since I've reset my Python interpreter, I need to import datetime again. 6 00:00:21,550 --> 00:00:24,762 And let's make an object that represents now, just so 7 00:00:24,762 --> 00:00:28,115 we don't have to keep typing out datetime.datetime.now. 8 00:00:28,115 --> 00:00:31,570 And we don't really care if we have new values or not. 9 00:00:31,570 --> 00:00:34,500 We just want something that's held on to here. 10 00:00:34,500 --> 00:00:38,900 Let's see if we can move this forward in time by three days. 11 00:00:38,900 --> 00:00:43,640 So, the way that we manipulate date times is with our timedeltas. 12 00:00:43,640 --> 00:00:49,263 So, let's make a timedelta and we can say in here like, hours equals 5. 13 00:00:49,263 --> 00:00:53,140 Okay, and I've got this thing back and you see it's 18,000 seconds. 14 00:00:54,160 --> 00:00:59,540 What I wanna do though is I want to make one of these that's days. 15 00:00:59,540 --> 00:01:07,190 So, I wanna do days equals we said 3, so 3. 16 00:01:07,190 --> 00:01:08,380 So we just got a three there. 17 00:01:08,380 --> 00:01:10,200 You notice we had the zero before. 18 00:01:10,200 --> 00:01:13,260 That's because it was zero days it was just five hours. 19 00:01:13,260 --> 00:01:19,020 Okay if I was to do now plus daytime.time delta days equals 3. 20 00:01:19,020 --> 00:01:26,433 Then where it was the 15th, it is now the 18th, okay? 21 00:01:26,433 --> 00:01:26,988 . 22 00:01:26,988 --> 00:01:29,440 Everything else is exactly the same, right? 23 00:01:29,440 --> 00:01:34,060 It's still 18 hours and 30 seconds or sorry, 18 hours and 30 minutes. 24 00:01:34,060 --> 00:01:35,432 Just the days changed. 25 00:01:35,432 --> 00:01:38,280 All right, what if I wanted to go back in time? 26 00:01:38,280 --> 00:01:44,050 So we could do now plus datetime.timedelta, 27 00:01:44,050 --> 00:01:46,429 days equals, lets go back five days. 28 00:01:47,740 --> 00:01:49,220 I did minus five days. 29 00:01:49,220 --> 00:01:51,480 So again, we went from the 15th to the 10th. 30 00:01:52,600 --> 00:01:57,080 Now, adding negative five days, that's a little confusing to some people. 31 00:01:57,080 --> 00:01:59,540 I can see why, it's a little strange. 32 00:01:59,540 --> 00:02:01,260 So what we can do is we can actually subtract. 33 00:02:01,260 --> 00:02:07,250 So, let's do now minus datetime.timedelta days equal 5. 34 00:02:07,250 --> 00:02:11,490 So we've subtracted five days, we get the exact same number. 35 00:02:11,490 --> 00:02:12,190 So it works. 36 00:02:12,190 --> 00:02:13,200 It gives us the same day. 37 00:02:13,200 --> 00:02:17,010 So we have two different ways to manipulate our datetimes. 38 00:02:17,010 --> 00:02:20,090 So what if we just want a date? 39 00:02:20,090 --> 00:02:22,550 Or we just want a time? 40 00:02:22,550 --> 00:02:23,250 I just want the date. 41 00:02:23,250 --> 00:02:24,630 I don't care about what time it is. 42 00:02:24,630 --> 00:02:29,200 So I can actually do now.date, and I get back a date. 43 00:02:29,200 --> 00:02:33,530 And I can do now.time and I get back a time. 44 00:02:33,530 --> 00:02:38,410 So these can be used if I just need the time, I just need the, the date, whatever. 45 00:02:38,410 --> 00:02:40,280 What else can we do with these, though? 46 00:02:40,280 --> 00:02:44,890 Well, we can work with multiple timedeltas, which is kinda cool. 47 00:02:44,890 --> 00:02:51,208 So let's make a timedelta here that's hour, which is a timedelta hours equals 1. 48 00:02:51,208 --> 00:02:52,700 So it's one hour long. 49 00:02:52,700 --> 00:02:55,390 3600 minute seconds. 50 00:02:55,390 --> 00:02:59,333 And we're gonna say a workday equals hour times 9. 51 00:02:59,333 --> 00:03:01,490 Cuz you got a lunch hour. 52 00:03:01,490 --> 00:03:05,610 So eight hour day plus one hour lunch gives you nine hours, right? 53 00:03:05,610 --> 00:03:12,605 Let's do tomorrow equals datetime.datetime.now.replace 54 00:03:12,605 --> 00:03:16,228 hour equals 9, minute equals 0. 55 00:03:16,228 --> 00:03:24,780 And then really, we should do plus datetime.timedelta days equal 1. 56 00:03:26,220 --> 00:03:30,180 So if we look at tomorrow, that's tomorrow morning at 9 a.m, more or 57 00:03:30,180 --> 00:03:32,680 less we've got a couple seconds in there, but whatever. 58 00:03:32,680 --> 00:03:33,500 If we did that and 59 00:03:33,500 --> 00:03:38,400 we want to say okay what time would I be off tomorrow, if it wasn't standard. 60 00:03:38,400 --> 00:03:43,195 We could do tomorrow plus work day and we get out 18:00. 61 00:03:43,195 --> 00:03:44,560 So six, 6 p.m. 62 00:03:44,560 --> 00:03:46,170 That's really nice. 63 00:03:46,170 --> 00:03:49,780 I, I can use this to calculate the ends of an appointment or something. 64 00:03:49,780 --> 00:03:52,380 I just need to make a timedelta as the length of the appointment. 65 00:03:52,380 --> 00:03:53,810 So let's see. 66 00:03:53,810 --> 00:03:59,980 We did appointment equals datetime.timedelta minutes equals 45. 67 00:03:59,980 --> 00:04:03,899 Maybe I have a standard 45 minute meeting slot that I give out. 68 00:04:03,899 --> 00:04:12,442 All right and we'll say the start is a datetime of the 1st of November and 69 00:04:12,442 --> 00:04:17,170 it's gonna be 45 minutes after noon. 70 00:04:17,170 --> 00:04:18,170 Okay? 71 00:04:18,170 --> 00:04:21,610 So, the end of that is going to be start plus appointment. 72 00:04:23,530 --> 00:04:24,960 And if we look at end, 73 00:04:24,960 --> 00:04:30,140 then we see that it ends at 1:30 which is 45 minutes after 12:45. 74 00:04:30,140 --> 00:04:34,187 >> Manipulating datetimes with timedelta is probably the best part of the datetime 75 00:04:34,187 --> 00:04:35,040 library. 76 00:04:35,040 --> 00:04:37,580 It's fairly simple and intuitive and it's really handy for 77 00:04:37,580 --> 00:04:39,080 a lot of real world scenarios.