Explain Codes LogoExplain Codes Logo

How do I turn a Python datetime into a string, with readable format date?

python
date-formatting
datetime
f-strings
Nikita BarsukovbyNikita Barsukov·Feb 13, 2025
TLDR

Quickly turn a datetime into a human-readable string using strftime with a format like "%Y-%m-%d %H:%M:%S" :

from datetime import datetime # Who needs time machines when you have Python? print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) # e.g., "2023-04-07 14:23:01"

Modify the format string to your preference.

Detailed walkthrough

Extracting atomic date components

Did you know that datetime objects are as sliceable as hot birthday cakes? Use dot notation to serve up the year, month, day, hour, minute, or second:

now = datetime.now() # This is your date on atomic level! formatted_date = f"{now.year}-{now.month:02d}-{now.day:02d} {now.hour:02d}:{now.minute:02d}:{now.second:02d}" print(formatted_date) # e.g., "2023-04-07 14:23:01"

Date formatting with strftime

strftime is your Pythonic date formatter. It uses format codes you can cherry-pick from the official Python docs (reference 1). Want a full-length month in your date or to add a comma? Use "%B %d, %Y":

# It's a date, not a math problem. print(datetime.now().strftime("%B %d, %Y")) # e.g., "April 07, 2023"

F-strings for concise date formatting

Python 3.6+ users, meet f-strings. These guys will let you embed any expression, including datetime objects, inside string literals:

# Work smarter, not harder. print(f"{datetime.now():%B %d, %Y}") # "April 07, 2023"

This mint example showcases the martini-dry syntax of f-strings for in-line date formatting.

Alternative methods and best practices

Quick and dirty ctime formatting

For something fast with no frills, the ctime() method of datetime objects will kick out a readable timestamp.

# Friday afternoon coding sessions be like: print(datetime.now().ctime()) # "Fri Apr 7 14:23:01 2023"

While not customizable, it's a life-saver when you just need the timestamp in a jiffy.

The strftime Vs format() duel

Faced with the strftime Vs format() question? It's really all about the application and requirements of your code. If you're already formatting multiple variables into a string, hold onto your seat:

current_time = datetime.now() # Where format shines. formatted_date = "{:%B %d, %Y}".format(current_time) print(formatted_date) # "April 07, 2023"

Warning: strftime arguments needed

When using strftime(), never forget a piece of the format or you'll end up with strings missing parts or even errors. Always ensure your format codes yield the desired output:

# Oops! Time fell off the edge here. print(datetime.now().strftime("%B %d, %Y")) # "April 07, 2023" (No time!) # Much better. print(datetime.now().strftime("%B %d, %Y %H:%M:%S")) # "April 07, 2023 14:23:01"

Roadblocks and power tools

Time zone considerations

When you bring up time zones, make sure your datetime object is timezone aware. Use %z in strftime for this:

from datetime import timezone # Wait, what time is it in 2023? now = datetime.now(timezone.utc) print(now.strftime("%Y-%m-%d %H:%M:%S%z")) # "2023-04-07 14:23:01+0000"

Localized date formatting

Need date names in a different language? Change the Python locale accordingly, but ensure it's supported by your system.

Complex date formatting with external libraries

When it comes to the bigger beasts of date formatting, Arrow or dateutil libraries (reference 6 & 7) got you covered. They bring new tools for date manipulation and formatting:

import arrow # Keep calm and format on. print(arrow.now().format('MMMM DD, YYYY')) # "April 07, 2023"