Discover how to effectively format dates and times using Python to display military time, AM/PM formats, and full or abbreviated dates. Then, apply conditional logic to craft timely greetings based on the current hour.
Key Insights
- Learned how to format time in Python using the
strftime()
method to represent military time (e.g., 18:28), standard AM/PM formats (e.g., 06:28 PM), and complete dates (e.g., Tuesday, March 25, 2025). - Explored conditional logic with Python's
if
,elif
, andelse
statements to generate dynamic greetings ("good morning," "good afternoon," "good evening") based on the current hour. - Understood the difference between properties (which return values without parentheses) and methods (functions called on an object, indicated by parentheses), emphasizing the importance of storing returned values in clearly defined variables.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
Now, what can we do with all this fun stuff? Let's do a little bit more delving into this. It's kind of useful. So what if you want to get the full-time in military format? That would be, in New York here, it's 1800 hours.
So you would use a capital H colon M, capital hour being the military hour. So let's see. We'll say hour.
Let's back up. So hour is 18, fine. We can also get the stringy version of that.
We could say hour equals my_datetime.strftime, big H, big H for hour. And that is an int. Wait, we're past that, sorry.
Oh, that's a string now, right? Yep. So because it's a string, strftime. That is, strftime will get you a string.
And then there's colon min, big M for minute. Let's say min, I guess M. Let's say H as well, kind of like that IMDb time, if you recall. There it is.
But that would be a big M. There you go. It's 628. They're both strings because it's strftime, string format time.
Now, if you put them together, we can just run them right together with one move. And we could just print the mil time as a string. Let's just call it mil time.
Boom, yep, 1800 hours and 28 minutes. Now, if you'd like the AM/PM version, which would be 628 PM, we're going to use—we already did this one. The AM/PM version would use an I and an M. So let's try that.
Let's just print—what do we get? my_datetime.strftime, I. What's that going to give you? An 06. All right, that would be the hour. And what would the M give you? We already know that will give you the minute, right? And then what would the little p give you? That'll give you the PM.
If we put it all together, we'll say AM/PM time, instead of military time. That's going to be my_datetime.strftime. And you just string all three of them together in one move.
Kaboom, 0628 PM. And let's get today's full date. That's A, B, A for the full, the capitalized day.
Big B for the capitalized month. Little d for the day of the month, the number, the int, rather.
And big Y for the four-digit year. So that would just be the day's—let's just say—full date. We get it that it's today, right? And that will be my_datetime.strftime, all four of these things.
And notice, you even have the commas, right? You want a comma after the day and the date. So you put it in, exactly what you want. There it is, comma, comma, today's date.
And the abbreviated date, right? Maybe we want TUE MAR, and then maybe only a two-digit year. So that would be your strftime. We'll call it abbreviated_date, my_datetime.strftime.
And notice, I'm escaping. I don't want to confuse you. Let's use double quotes.
And then there's an interior single quote because it's a two-digit year, right? That 25, if you only do two-digit years, quite often you'll see an apostrophe, right? You're not going to just see the number in all likelihood. So that's how you do the little date. Now, with all that under our belt, what I need you to do, I need you to pause and go back and do all of it.
I know, I know. How else are you supposed to learn to program? Why would I ever use this? You're going to need it in this lesson, okay? You're going to need it at some point. You're just going to get better at programming in general if you do this.
It's not really even about this particular subtopic. It's about logging your hours, okay? You got to put in your hours of programming, typing. You'll get smoother and faster and you'll remember things and you'll get a handle.
Some stuff will start to click, like, huh, sometimes we call a function at the end and pass in a value. Sometimes we just put a word with no parentheses at the end. What's the difference? Well, the difference is the one with no parentheses is called a property.
It just returns a value. And the other one with the parentheses, the strftime, is a method. It's a function called on an object, hence a method.
And in both cases, it returns a value, which is why we're always setting these operations equal to a variable to catch the return value. In other words, to store the answer somewhere. We're not just printing the answer, right? We're storing it in a variable.
The name container that holds a value, right? Like here, we just printed these, but then we stored all three of them together. All right, challenge. Make a timely greeting based on the current hour.
In other words, you're going to use if, elif, else logic to check the current hour. And if it's before 12 o'clock, you're going to make a greeting that says good morning. If that's false, it's after 12 o'clock, which it is here in New York.
You're going to check to see if it's before 1800 hours, in which case you're going to say good afternoon or 1900 hours. It's still daylight outside. I guess you could say good afternoon to people, although it's kind of like good evening time.
And if it's after, say, 1800 hours, we could say good evening. So I'd like you to try to write—it's an if-elif-else. And in all cases, you're setting a variable called greeting.
Make a timely greeting based on the current hour. Don't just print the greeting, save it to a variable. In other words, declare a variable called greeting.
Let's go ahead and do that part. And we'll set greeting equal to an empty string. In other words, just text.
We know it's going to get a value. We don't know what the value is yet, so we'll just start with the empty string. Okay, stop the recording, give it a try, and come back for the solution.
Okay, we're back with the solution for the 03 lesson challenge timely greeting. We've got our empty greeting variable, and we're going to start with an—so we'll start with our empty string, okay? And move that down. And now we're going to check.
We're going to say if, let's get the current hour again. We'll say hour equals my_datetime.hour. Let's just print the hour, make sure it's working.
You can label it, whoa, invalid syntax. Oh yeah, the if can't just sit there like that. Okay, the hour is 18, great.
Now we're going to say if hour is less than 12, which is false, we're going to set greeting equal to good morning, right? And that's false; we're going to move on. Now we're going to check the 12 to 1800 range, right? If hour less than 12 is false, we would next check to see elif hour is less than 18. And if that's the case, if that's true, we're going to set the greeting variable equal to good afternoon.
Else, if that's false, hour is in the 18 to 23 range, so it's the evening. That is the actual case right now. At least at this moment in time.
And then we're going to print the greeting. We're getting good evening, right? Because if hour is less than 12 is false, if hour is less than 18 is false, because it is 18. Now we could also just come in here with a less than or equal and suddenly it's good afternoon.
Because now it's true, hour is 18 and we're asking if hour is less than or equal to 18, which is suddenly true. And now we're saying good morning, good afternoon. And we could even force the issue; we could say, okay, hour is seven.
Now we're pretending it's morning, right? With a fake, like a testing hour. And there's the real hour, right? Get a fresh hour here. Because the hour might've changed, right? It's been a little while since we grabbed the hour, and we never called it hour; we call it HR.
So the hour, once you grab the variable, it's not going to auto-refresh or anything. So it might get a little old, like the minute could get old, right? A minute later. So that is the answer to this challenge.
And that, just like so—if, elif, else. Now onto the next module; that's datetime for you.