Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Dating Methods7:28 with Kenneth Love
Let's see about turning your `datetime` objects into specific strings, or creating `datetime` objects from strings.
So what if you want your date and time in a certain format, 0:00 like you need to print that out all pretty for a script or on your blog. 0:04 Welcome to the warm, wonderful world of date formatting. 0:07 I've put a link in the teacher's notes. 0:10 You should probably have this open for the video and the later code challenges. 0:12 Trust me, no one memorizes this. 0:16 But complicated or not, it's one of the most useful areas in the datetime library. 0:18 So as you can see, I jumped ahead a little bit. 0:23 I've already got datetime imported and I've already created our now variable. 0:25 Just like we did in the last video. 0:28 So lets look at our object once again. 0:30 We get this datetime.datetime which tells us the class and everything and 0:34 then we get this spelled out big long thing of year, 0:38 month, day, hour, minute, second, microsecond. 0:41 It's definitely readable, but do we wanna look at that every single time? 0:44 Do we have to look at that every single time? 0:47 Do we have to pull apart the month and 0:50 the year attributes in order to get out our, you know, nice, pretty strings? 0:52 No, of course we don't. 0:56 Python's not gonna make us do all that extra work. 0:58 So let's look at this function called strftime. 1:00 strftime let's us turn a date, a time, or 1:04 a datetime into a string formatted how we want. 1:07 So let's use strftime and let's get out just the month name and the day number. 1:11 So we do now.strftime, and then we have to pass in a string. 1:17 So %B is the month name. 1:21 I'm gonna do a space and I'm gonna %d. 1:25 And print that and we get October 15th. 1:28 So that's handy enough but there's a problem. 1:31 And the problem is that I don't know all of these strings. 1:34 So let's come down here and let's actually go to 1:40 docs.python.org as we do, and let's search over here for datetime. 1:44 And here's our datetime module, and if we look down here, 1:50 we have this strftime and strptime behavior. 1:54 And there's this awesome, awesome table here showing me everything. 1:59 So you can see I did %B, and I got the full name in my current local. 2:01 So if I was in Germany, I'd get a different name. 2:06 If I was in Spain, I'd get a different name, so on and so forth. 2:09 Since I'm in the US, I get January, February, whatever. 2:12 Okay, so what I want is I want what we consider like a US 2:15 standard time stamp, which is one where it's month, day, year. 2:20 Okay. 2:26 So now.strftime, and I have to pass in a string. 2:26 So let's find month. 2:30 Here it is. 2:33 Month as zero-padded decimal number. 2:34 So that's %m, okay, %m. 2:35 And I'm gonna do a slash and I'm gonna do %d is the day. 2:39 Cuz I did that before and then now I want the year. 2:42 And I want, well, so let's see. 2:45 We have here year without century so we get like 01 or 2:48 14 which is what we're gonna get. 2:53 Or year with century and we'd get a longer number like 2013, 2014. 2:55 Let's do the shorter number. 3:00 And we're going to do %y. 3:01 [NOISE] We press Return and we get 10/15/14. 3:04 All right, so that's it. 3:09 The hardest part of this is really referring back to those datetime docs. 3:11 Finding exactly which one you want in this table is not always the easiest thing. 3:15 Sometimes it takes a little bit of reading to figure out which one that you're doing. 3:21 So bookmark this, keep it handy, you're gonna be using this a lot. 3:26 It's definitely a very, very useful thing to have around. 3:29 I mentioned that we're also gonna talk about an strptime function. 3:33 Now sadly, strftime and strptime are nearly identical. 3:38 There's one letter difference, F or P. 3:43 So, you're probably gonna end up confusing them. 3:45 I know I do. 3:47 But I've started thinking of them in this way, strftime is string from time, 3:49 or string formatted time. 3:54 And strptime is string parsed into time. 3:56 So, hopefully that pneumonic, depending on how good it is, will help you. 4:00 So what does strptime do? 4:05 I keep saying it, we keep talking about it, but 4:07 we haven't talked about what it does yet. 4:09 So it let's us make a datetime from a string with a certain format. 4:11 Okay, so, let's do this. 4:15 Let's say birthday, datetime.strptime and 4:17 then we pass in a string that represents the date. 4:23 So let's do 2015-04-21. 4:26 Which would be the 21st of April next year. 4:30 This isn't my birthday, just a random date. 4:34 All right, so, but now what I have to do is I have to explain 4:37 to datetime how this string is formatted. 4:42 And I do that with the same string types that we used in strftime. 4:45 So, I remember that %Y is the year. 4:50 Let's look that up just to make sure, and there it is. 4:54 That's the long year. 4:58 Okay. 4:59 And then we do hyphen and we do %m, hyphen, %d, all right. 5:00 So the only thing different here is kind of the order they're in, and 5:04 we used a cap Y instead of a lower case Y. 5:08 Oh, sorry, it is datetime.datetime. 5:09 [BLANK_AUDIO] 5:12 And I missed my dot. 5:17 There we go, okay. 5:21 So now, we should be able to look at birthday and 5:23 there's our datetime that was created. 5:26 Now, you notice there's no there's no hours on this because we didn't have any 5:28 hours specified in our strptime. 5:33 But that only had the date. 5:35 What if we wanted to include a time as well? 5:36 So let's do birthday_party. 5:39 Because this is when the party is. 5:41 So datetime.datetime.strptime. 5:44 And we'll do 2015 again, and 04 again, and 21. 5:47 And then we'll do a space, and then we'll do 09, colon, 00. 5:52 Okay, so this is a lot more to explain. 5:57 We're having the birthday party, you know what? 6:00 Let's have the birthday party at noon instead. 6:01 All right, how do we describe this one? 6:05 Well, the beginning is still the same. 6:06 So it's that. And then we put in a literal space. 6:09 And then I need hours. 6:13 And, let's see. 6:17 Hours are capital H, all right. 6:18 Capital H, colon, and I know that capital M is minutes. 6:23 You start to memorize some of these as you go through. 6:28 And now let's look at birthday_party. 6:31 And there's our time and it's set at noon. 6:34 All right, so we have everything we put in there. 6:36 Now, I want you to notice, I did the capital H so it's a 24-hour clock. 6:39 So like 1 PM would be 13, 6 PM would be 18. 6:43 I do that so I don't have to specify AM or PM. 6:47 There is a flag you can put in for AM and PM if you wanna use those, 6:50 if that's more comfortable to you. 6:53 So you have full control of this. 6:55 Turning datetime objects into strings, or strings into datetimes, 6:58 is mostly a matter of remembering or looking up the format strings. 7:01 I'm sure you can see it's also pretty handy. 7:05 Actually, I have a challenge for you now. 7:08 Using the Python docs, and strftime and 7:10 strptime, make a script that accepts a month and 7:13 day date in whatever format you want that returns a link to Wikipedia for that date. 7:16 Be sure you tell the user how to format their data for you. 7:21 Come back in the next video and we'll build it together. 7:24
You need to sign up for Treehouse in order to download course files.Sign up