1 00:00:00,610 --> 00:00:04,060 So what if you want your date and time in a certain format, 2 00:00:04,060 --> 00:00:07,770 like you need to print that out all pretty for a script or on your blog. 3 00:00:07,770 --> 00:00:10,770 Welcome to the warm, wonderful world of date formatting. 4 00:00:10,770 --> 00:00:12,480 I've put a link in the teacher's notes. 5 00:00:12,480 --> 00:00:16,350 You should probably have this open for the video and the later code challenges. 6 00:00:16,350 --> 00:00:18,680 Trust me, no one memorizes this. 7 00:00:18,680 --> 00:00:22,269 But complicated or not, it's one of the most useful areas in the datetime library. 8 00:00:23,670 --> 00:00:25,480 So as you can see, I jumped ahead a little bit. 9 00:00:25,480 --> 00:00:28,770 I've already got datetime imported and I've already created our now variable. 10 00:00:28,770 --> 00:00:30,640 Just like we did in the last video. 11 00:00:30,640 --> 00:00:34,540 So lets look at our object once again. 12 00:00:34,540 --> 00:00:38,050 We get this datetime.datetime which tells us the class and everything and 13 00:00:38,050 --> 00:00:41,010 then we get this spelled out big long thing of year, 14 00:00:41,010 --> 00:00:44,140 month, day, hour, minute, second, microsecond. 15 00:00:44,140 --> 00:00:47,730 It's definitely readable, but do we wanna look at that every single time? 16 00:00:47,730 --> 00:00:50,330 Do we have to look at that every single time? 17 00:00:50,330 --> 00:00:52,160 Do we have to pull apart the month and 18 00:00:52,160 --> 00:00:56,470 the year attributes in order to get out our, you know, nice, pretty strings? 19 00:00:56,470 --> 00:00:58,230 No, of course we don't. 20 00:00:58,230 --> 00:01:00,440 Python's not gonna make us do all that extra work. 21 00:01:00,440 --> 00:01:04,315 So let's look at this function called strftime. 22 00:01:04,315 --> 00:01:07,130 strftime let's us turn a date, a time, or 23 00:01:07,130 --> 00:01:11,100 a datetime into a string formatted how we want. 24 00:01:11,100 --> 00:01:17,380 So let's use strftime and let's get out just the month name and the day number. 25 00:01:17,380 --> 00:01:21,452 So we do now.strftime, and then we have to pass in a string. 26 00:01:21,452 --> 00:01:25,620 So %B is the month name. 27 00:01:25,620 --> 00:01:28,325 I'm gonna do a space and I'm gonna %d. 28 00:01:28,325 --> 00:01:31,310 And print that and we get October 15th. 29 00:01:31,310 --> 00:01:34,200 So that's handy enough but there's a problem. 30 00:01:34,200 --> 00:01:39,770 And the problem is that I don't know all of these strings. 31 00:01:40,945 --> 00:01:44,745 So let's come down here and let's actually go to 32 00:01:44,745 --> 00:01:50,500 docs.python.org as we do, and let's search over here for datetime. 33 00:01:50,500 --> 00:01:54,790 And here's our datetime module, and if we look down here, 34 00:01:54,790 --> 00:01:59,050 we have this strftime and strptime behavior. 35 00:01:59,050 --> 00:02:01,960 And there's this awesome, awesome table here showing me everything. 36 00:02:01,960 --> 00:02:06,510 So you can see I did %B, and I got the full name in my current local. 37 00:02:06,510 --> 00:02:09,420 So if I was in Germany, I'd get a different name. 38 00:02:09,420 --> 00:02:12,280 If I was in Spain, I'd get a different name, so on and so forth. 39 00:02:12,280 --> 00:02:15,000 Since I'm in the US, I get January, February, whatever. 40 00:02:15,000 --> 00:02:20,340 Okay, so what I want is I want what we consider like a US 41 00:02:20,340 --> 00:02:26,040 standard time stamp, which is one where it's month, day, year. 42 00:02:26,040 --> 00:02:26,560 Okay. 43 00:02:26,560 --> 00:02:30,000 So now.strftime, and I have to pass in a string. 44 00:02:30,000 --> 00:02:32,350 So let's find month. 45 00:02:33,510 --> 00:02:34,180 Here it is. 46 00:02:34,180 --> 00:02:35,930 Month as zero-padded decimal number. 47 00:02:35,930 --> 00:02:39,930 So that's %m, okay, %m. 48 00:02:39,930 --> 00:02:42,700 And I'm gonna do a slash and I'm gonna do %d is the day. 49 00:02:42,700 --> 00:02:45,720 Cuz I did that before and then now I want the year. 50 00:02:45,720 --> 00:02:48,380 And I want, well, so let's see. 51 00:02:48,380 --> 00:02:53,260 We have here year without century so we get like 01 or 52 00:02:53,260 --> 00:02:55,120 14 which is what we're gonna get. 53 00:02:55,120 --> 00:03:00,259 Or year with century and we'd get a longer number like 2013, 2014. 54 00:03:00,259 --> 00:03:01,618 Let's do the shorter number. 55 00:03:01,618 --> 00:03:04,129 And we're going to do %y. 56 00:03:04,129 --> 00:03:09,326 [NOISE] We press Return and we get 10/15/14. 57 00:03:09,326 --> 00:03:11,690 All right, so that's it. 58 00:03:11,690 --> 00:03:15,810 The hardest part of this is really referring back to those datetime docs. 59 00:03:15,810 --> 00:03:21,880 Finding exactly which one you want in this table is not always the easiest thing. 60 00:03:21,880 --> 00:03:26,380 Sometimes it takes a little bit of reading to figure out which one that you're doing. 61 00:03:26,380 --> 00:03:29,890 So bookmark this, keep it handy, you're gonna be using this a lot. 62 00:03:29,890 --> 00:03:32,900 It's definitely a very, very useful thing to have around. 63 00:03:33,980 --> 00:03:38,150 I mentioned that we're also gonna talk about an strptime function. 64 00:03:38,150 --> 00:03:43,410 Now sadly, strftime and strptime are nearly identical. 65 00:03:43,410 --> 00:03:45,460 There's one letter difference, F or P. 66 00:03:45,460 --> 00:03:47,750 So, you're probably gonna end up confusing them. 67 00:03:47,750 --> 00:03:49,040 I know I do. 68 00:03:49,040 --> 00:03:54,860 But I've started thinking of them in this way, strftime is string from time, 69 00:03:54,860 --> 00:03:56,700 or string formatted time. 70 00:03:56,700 --> 00:04:00,550 And strptime is string parsed into time. 71 00:04:00,550 --> 00:04:05,670 So, hopefully that pneumonic, depending on how good it is, will help you. 72 00:04:05,670 --> 00:04:07,580 So what does strptime do? 73 00:04:07,580 --> 00:04:09,240 I keep saying it, we keep talking about it, but 74 00:04:09,240 --> 00:04:11,040 we haven't talked about what it does yet. 75 00:04:11,040 --> 00:04:15,910 So it let's us make a datetime from a string with a certain format. 76 00:04:15,910 --> 00:04:17,560 Okay, so, let's do this. 77 00:04:17,560 --> 00:04:23,380 Let's say birthday, datetime.strptime and 78 00:04:23,380 --> 00:04:26,960 then we pass in a string that represents the date. 79 00:04:26,960 --> 00:04:30,990 So let's do 2015-04-21. 80 00:04:30,990 --> 00:04:34,660 Which would be the 21st of April next year. 81 00:04:34,660 --> 00:04:37,382 This isn't my birthday, just a random date. 82 00:04:37,382 --> 00:04:42,210 All right, so, but now what I have to do is I have to explain 83 00:04:42,210 --> 00:04:45,890 to datetime how this string is formatted. 84 00:04:45,890 --> 00:04:50,594 And I do that with the same string types that we used in strftime. 85 00:04:50,594 --> 00:04:54,310 So, I remember that %Y is the year. 86 00:04:54,310 --> 00:04:58,420 Let's look that up just to make sure, and there it is. 87 00:04:58,420 --> 00:04:59,540 That's the long year. 88 00:04:59,540 --> 00:05:00,820 Okay. 89 00:05:00,820 --> 00:05:04,960 And then we do hyphen and we do %m, hyphen, %d, all right. 90 00:05:04,960 --> 00:05:08,019 So the only thing different here is kind of the order they're in, and 91 00:05:08,019 --> 00:05:09,781 we used a cap Y instead of a lower case Y. 92 00:05:09,781 --> 00:05:12,282 Oh, sorry, it is datetime.datetime. 93 00:05:12,282 --> 00:05:17,788 [BLANK_AUDIO] 94 00:05:17,788 --> 00:05:19,330 And I missed my dot. 95 00:05:21,510 --> 00:05:23,010 There we go, okay. 96 00:05:23,010 --> 00:05:26,880 So now, we should be able to look at birthday and 97 00:05:26,880 --> 00:05:28,730 there's our datetime that was created. 98 00:05:28,730 --> 00:05:33,229 Now, you notice there's no there's no hours on this because we didn't have any 99 00:05:33,229 --> 00:05:35,050 hours specified in our strptime. 100 00:05:35,050 --> 00:05:36,390 But that only had the date. 101 00:05:36,390 --> 00:05:39,190 What if we wanted to include a time as well? 102 00:05:39,190 --> 00:05:41,710 So let's do birthday_party. 103 00:05:41,710 --> 00:05:44,080 Because this is when the party is. 104 00:05:44,080 --> 00:05:47,820 So datetime.datetime.strptime. 105 00:05:47,820 --> 00:05:52,122 And we'll do 2015 again, and 04 again, and 21. 106 00:05:52,122 --> 00:05:57,520 And then we'll do a space, and then we'll do 09, colon, 00. 107 00:05:57,520 --> 00:06:00,130 Okay, so this is a lot more to explain. 108 00:06:00,130 --> 00:06:01,064 We're having the birthday party, you know what? 109 00:06:01,064 --> 00:06:03,470 Let's have the birthday party at noon instead. 110 00:06:05,000 --> 00:06:06,763 All right, how do we describe this one? 111 00:06:06,763 --> 00:06:08,480 Well, the beginning is still the same. 112 00:06:09,830 --> 00:06:13,090 So it's that. And then we put in a literal space. 113 00:06:13,090 --> 00:06:14,860 And then I need hours. 114 00:06:17,450 --> 00:06:18,120 And, let's see. 115 00:06:18,120 --> 00:06:23,769 Hours are capital H, all right. 116 00:06:23,769 --> 00:06:28,640 Capital H, colon, and I know that capital M is minutes. 117 00:06:28,640 --> 00:06:31,950 You start to memorize some of these as you go through. 118 00:06:31,950 --> 00:06:34,464 And now let's look at birthday_party. 119 00:06:34,464 --> 00:06:36,950 And there's our time and it's set at noon. 120 00:06:36,950 --> 00:06:39,900 All right, so we have everything we put in there. 121 00:06:39,900 --> 00:06:43,500 Now, I want you to notice, I did the capital H so it's a 24-hour clock. 122 00:06:43,500 --> 00:06:47,310 So like 1 PM would be 13, 6 PM would be 18. 123 00:06:47,310 --> 00:06:50,140 I do that so I don't have to specify AM or PM. 124 00:06:50,140 --> 00:06:53,920 There is a flag you can put in for AM and PM if you wanna use those, 125 00:06:53,920 --> 00:06:55,220 if that's more comfortable to you. 126 00:06:55,220 --> 00:06:58,442 So you have full control of this. 127 00:06:58,442 --> 00:07:01,990 Turning datetime objects into strings, or strings into datetimes, 128 00:07:01,990 --> 00:07:05,590 is mostly a matter of remembering or looking up the format strings. 129 00:07:05,590 --> 00:07:08,220 I'm sure you can see it's also pretty handy. 130 00:07:08,220 --> 00:07:10,280 Actually, I have a challenge for you now. 131 00:07:10,280 --> 00:07:13,438 Using the Python docs, and strftime and 132 00:07:13,438 --> 00:07:16,490 strptime, make a script that accepts a month and 133 00:07:16,490 --> 00:07:21,910 day date in whatever format you want that returns a link to Wikipedia for that date. 134 00:07:21,910 --> 00:07:24,700 Be sure you tell the user how to format their data for you. 135 00:07:24,700 --> 00:07:26,690 Come back in the next video and we'll build it together.