Formatting time with Python made simple

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.

Python time clock

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:

%Yyear2023
%mmonth01
%dday20
%Hhour12
%Mminute37
%Sseconds27
basic format options

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

%H24-hour format, 2-digit hours with leading zero00, 01, 02, … 23
%-H24-hour format, hours without leading zero0, 1, 2, … 23
%I12-hour format, 2-digit hours with leading zero00, 01, 02, … 12
%-I12-hour format, hours without leading zero0, 1, 2, … 12
%pAM or PMAM, PM
%M2-digit minutes with leading zero00, 01, 02, … 59
%-Mminutes without leading zero0, 1, 2, … 59
%S2-digit seconds with leading zero00, 01, 02, … 59
%-Sseconds without leading zero0, 1, 2, … 59

Date formatters

%Yfull 4-digit year1999, 2000, 2009, 2023
%y2-digit short year, with leading zero99, 00, 09, 23
%-yshort year without leading zero99, 0, 9, 23
%m2-digit months with leading zero01, 02, 03, … 12
%-mmonths without leading zero1, 2, 3, … 12
%Bfull month nameJanuary, February, …
%bshort month nameJan, Feb, …
%d2-digit days with leading zero01, 02, 03, … 31
%-ddays without leading zero1, 2, 3, … 31

Extra formatters

%fmicroseconds000000 – 999999
%Afull day of the weekMonday, Tuesday, …
%ashort day of the weekMon, Tue, …
%wday of the week as a number (beware: not 1-7)0 – 6
%uday of the week as a number1 – 7
%j3-digit day of the year as a number with leading zeros001, 002, … 366
%-jday of the year as a number1, 2, … 366
%U2-digit week of the year, starting with Sunday, with leading zero00, 01, 02, … 53
%-Uweek of the year, starting with Sunday, without leading zero0, 1, 2, … 53
%W2-digit week of the year, starting with Monday, with leading zero00, 01, 02, … 53
%-Wweek of the year, starting with Monday, without leading zero0, 1, 2, … 53
%V2-digit ISO 8601 week of the year with leading zero01, 02, 03, … 53
%-VISO 8601 week of the year without leading zero1, 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:

%clocale-adjusted date and time formatTue Aug 16 21:30:00 1988 (en_US)
Di 16 Aug 21:30:00 1988 (de_DE)
%xlocale-adjusted date08/16/1988 (en_US)
16.08.1988 (de_DE)
%Xlocale-adjusted time21: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.

Garfield hates mondays Python time
Don’t hate Mondays, I plan to release videos every Monday!

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.