If we need to print the current time and date, we can choose from pretty easy and fast solutions, while it’s still possible to fine-tune every aspect.
Just print the current time!
Let’s start with getting the current time. When you run your Python program, you can ask the operating system about the date and time too. To do this, use the builtin datetime
library:
import datetime
now = datetime.datetime.now()
print(now)
The result is there: 2023-01-20 12:37:27.359950
– but it’s not quite right for everyone’s taste! It’s almost like the ISO 8601 standard, but the US date format would look like this: 01-20-2023. The French would write it like this: 20/01/2023, the Spanish would write 20-01-23, and the list can go on! We must format the output, luckily the strftime()
function can customize it! Just make a “clock”, hours and minutes. It would look like this:
import datetime
now = datetime.datetime.now()
print(now.strftime("%H:%M"))
With this, our output will look like this: 12:37
.
Basic time formatting with strftime()
Formatting is really similar to regular formatter strings: you have a string, and the Python interpreter will replace the special placeholders with their correct value. The difference is that these formatters don’t use {}, but %. These placeholders can be:
%Y | year | 2023 |
%m | month | 01 |
%d | day | 20 |
%H | hour | 12 |
%M | minute | 37 |
%S | seconds | 27 |
With this, try to make your custom time and date format!
Advanced time formatting with strftime()
Of course, this list is not finished at all! Where are the 12-hour formats? And where are the month names? Let’s group the formatters.
Time formatters
%H | 24-hour format, 2-digit hours with leading zero | 00, 01, 02, … 23 |
%-H | 24-hour format, hours without leading zero | 0, 1, 2, … 23 |
%I | 12-hour format, 2-digit hours with leading zero | 00, 01, 02, … 12 |
%-I | 12-hour format, hours without leading zero | 0, 1, 2, … 12 |
%p | AM or PM | AM, PM |
%M | 2-digit minutes with leading zero | 00, 01, 02, … 59 |
%-M | minutes without leading zero | 0, 1, 2, … 59 |
%S | 2-digit seconds with leading zero | 00, 01, 02, … 59 |
%-S | seconds without leading zero | 0, 1, 2, … 59 |
Date formatters
%Y | full 4-digit year | 1999, 2000, 2009, 2023 |
%y | 2-digit short year, with leading zero | 99, 00, 09, 23 |
%-y | short year without leading zero | 99, 0, 9, 23 |
%m | 2-digit months with leading zero | 01, 02, 03, … 12 |
%-m | months without leading zero | 1, 2, 3, … 12 |
%B | full month name | January, February, … |
%b | short month name | Jan, Feb, … |
%d | 2-digit days with leading zero | 01, 02, 03, … 31 |
%-d | days without leading zero | 1, 2, 3, … 31 |
Extra formatters
%f | microseconds | 000000 – 999999 |
%A | full day of the week | Monday, Tuesday, … |
%a | short day of the week | Mon, Tue, … |
%w | day of the week as a number (beware: not 1-7) | 0 – 6 |
%u | day of the week as a number | 1 – 7 |
%j | 3-digit day of the year as a number with leading zeros | 001, 002, … 366 |
%-j | day of the year as a number | 1, 2, … 366 |
%U | 2-digit week of the year, starting with Sunday, with leading zero | 00, 01, 02, … 53 |
%-U | week of the year, starting with Sunday, without leading zero | 0, 1, 2, … 53 |
%W | 2-digit week of the year, starting with Monday, with leading zero | 00, 01, 02, … 53 |
%-W | week of the year, starting with Monday, without leading zero | 0, 1, 2, … 53 |
%V | 2-digit ISO 8601 week of the year with leading zero | 01, 02, 03, … 53 |
%-V | ISO 8601 week of the year without leading zero | 1, 2, 3, … 53 |
%% | if you REALLY need to have a ‘%’ in your string | % |
Localized time formatting with strftime()
Okay, these are nice and all, but these are useful for manual time formatting. If you are lucky, many people will use your program, from all around the world. This means they would like to see their usual format, and Python gives us nice builtin solutions, so using these is preferable in many cases:
%c | locale-adjusted date and time format | Tue Aug 16 21:30:00 1988 (en_US) Di 16 Aug 21:30:00 1988 (de_DE) |
%x | locale-adjusted date | 08/16/1988 (en_US) 16.08.1988 (de_DE) |
%X | locale-adjusted time | 21:30:00 (en_US) 21:30:00 (de_DE) |
These formatters automatically apply each computer’s locale settings. This is also true for some other options, but these are mostly obvious: Garfield saying “Monday がきらいです” is a bit off.
The affected formatters are: weekdays (short and full), months (short and full) and AM/PM.
Learn more!
Check out the YouTube channel for more content and my Python course, if you would like to learn more.