- In the previous example we saw how to retrieve basic date and time information in Python. So now we'll take a look at how to format that information using a set of predefined string control codes. In your editor, open up the formatting_start file and in this example file you can see I've already set up the import statement to import the datetime class, so you don't need to do that. Python provides a standard set of string formatting codes that you can use in a variety of scenarios.
And if you've ever done any programming in C or a C++ and you've used the standard C library, then these codes might look familiar to you. If not, they're pretty easy to understand once you've seen them. So let's start with a familiar piece of code to get the current date and time. So I'll write now = datetime.now and we saw this in the previous example. So now that I've got the current date and time, let's take a look at some of the options I have available to me to format the output.
To format date information you use the stirftime function which is available as a method on the datetime object that is returned by the now function. So I'll just simply write print(now.strftime()) So this function takes a string argument that contains one or more of these codes as placeholders. So they function as placeholders for date and time data.
For example, to format a string with the full year number I'd use a string like this The current year is: and then %Y Just cut that and put it down here in the formatting section. So let's just go ahead and run that. I'll bring up my debugger and show the output window and so we'll run it and you can see the current year is 2017. I can also mix these codes with regular text as you can see here.
So this is just a regular string and it happens to have a placeholder in it. Let's get a little more fancy and use several codes in the string at once. So now, let's put in %a, %d and then upper-case %B and then lower-case %y This formatting string will print the abbreviated day name, followed by the day of the month and a full month name and then an abbreviated two-digit year.
So let's run that. And you can see that that's exactly what happened. Python also gives you a way of printing out locale specific information. Rather than having to manually figure it out, the specific locale your application is being run in and how to print out information using the current localized versions of date and times, there is a set of codes that do that for you. I'll enter some code to try that out. So here I'll write print(now.strftime( and the first one I'll write is Locale date and time: which is %c and then I'll copy this and make a couple of copies.
I'll just use the local date for the next one which is %x and then finally just the local time which is upper-case %X Alright. Let me put comment that line. So let's give that a try. And you can see it's printing out the date and time information appropriately for how I have my locale set up on my computer. Now if I was running this on a computer in a place such as Europe, then the information you see printed here would probably look a little bit different based on what the locale settings are.
For example, in the date I've got the month and then the day and then the year. But other countries put the day of the month first. So these control codes C, X, and upper-case X allow you to use whatever the current locale's appropriate formatting is for dates and times. Alright, let's finish up with a look at time formatting. So just the same way that you can print formatted date information, you can print formatted time information as well.
So I'll comment out these examples and I'll add an example to exercise the time option. Alright so I'll use the strftime and this time I'll write Current time: and I'll use %I: and then with a colon %M uppercase M and then %S and then a lower-case %p and then I'll make a copy of that and I'll try another one.
And then the next one will show 24-hour time: and I'll use %H and %M and I won't need the P for that one. So the codes I'm using here control how time is formatted. The upper-case I and H are for 12 and 24 hour clocks, the M is for minutes, S is for seconds, and the lower-case P is for a.m. or p.m. Let's run that.
And you can see that it's printing out 3:25 p.m. And here it's printing out 15:25 p.m. So on the 24-hour clock that's 3:25 p.m. using 24-hour based notation. So if you find yourself needing to print formatted date and time data, Python provides some pretty rich controls for accomplishing this scenario.
Updated
10/7/2020Released
1/30/2018- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML
Share this video
Embed this video
Video: Formatting time output