Last modified: May 04, 2026 By Alexander Williams
Python Datetime to String Guide
Converting a Python datetime object to a string is a common task. You often need to display dates in a readable format. You might also need to save dates to a file or send them in an API. The strftime method is your main tool for this.
This article will show you how to use strftime effectively. We will cover format codes, examples, and common pitfalls. By the end, you will be able to convert any datetime to a string with confidence.
What is strftime?
strftime stands for "string format time". It is a method available on Python datetime objects. It takes a format string as an argument. This format string contains special codes that represent parts of the date and time.
For example, %Y represents the four-digit year. %m represents the month as a zero-padded number. %d represents the day of the month.
You call strftime on a datetime object like this: datetime_object.strftime(format_string). The result is a string.
Basic Example: Current Date and Time
Let's start with a simple example. We will get the current date and time using datetime.now(). Then we will convert it to a string.
from datetime import datetime
# Get the current date and time
now = datetime.now()
# Convert to a string using a basic format
date_string = now.strftime("%Y-%m-%d %H:%M:%S")
print(date_string)
2025-03-15 14:30:45
In this example, %Y gives the full year, %m gives the month, %d gives the day, %H gives the hour in 24-hour format, %M gives minutes, and %S gives seconds. The hyphens and colons are literal characters.
Common Format Codes
Here are the most useful format codes for strftime:
- %Y: Four-digit year (e.g., 2025)
- %m: Zero-padded month (01 to 12)
- %d: Zero-padded day (01 to 31)
- %H: Hour in 24-hour format (00 to 23)
- %I: Hour in 12-hour format (01 to 12)
- %M: Minute (00 to 59)
- %S: Second (00 to 59)
- %p: AM or PM
- %A: Full weekday name (e.g., Saturday)
- %B: Full month name (e.g., March)
- %j: Day of the year (001 to 366)
- %w: Weekday as number (0 for Sunday, 6 for Saturday)
You can combine these codes with any text you want. The result will be a human-readable string.
Example: Readable Date Format
Let's create a more readable date string. We will use weekday and month names.
from datetime import datetime
now = datetime.now()
# Create a readable string
date_string = now.strftime("%A, %B %d, %Y at %I:%M %p")
print(date_string)
Saturday, March 15, 2025 at 02:30 PM
This format is perfect for user interfaces. It uses full names and a 12-hour clock with AM/PM.
Converting a Specific Date
You can also convert any datetime object, not just the current time. For example, you might have a date from a database or user input.
from datetime import datetime
# Create a specific datetime object
my_date = datetime(2024, 12, 25, 10, 30, 0)
# Convert to string
date_string = my_date.strftime("%Y-%m-%d")
print(date_string)
2024-12-25
This is useful for storing dates in a consistent format like ISO 8601. The %Y-%m-%d pattern is widely used for sorting and databases.
Using strftime with Time Only
If you only have a time object, you can still use strftime. The method works on time objects and datetime objects.
from datetime import time
# Create a time object
t = time(14, 30, 45)
# Format it
time_string = t.strftime("%H:%M:%S")
print(time_string)
14:30:45
This is helpful when you need to display only the time part of a schedule or log entry.
Locale-Specific Formatting
Python's strftime respects your system's locale. This means month and day names will appear in your local language. You can change the locale using the locale module.
import locale
from datetime import datetime
# Set locale to French (if available on your system)
locale.setlocale(locale.LC_TIME, 'fr_FR.UTF-8')
now = datetime.now()
date_string = now.strftime("%A, %d %B %Y")
print(date_string)
samedi, 15 mars 2025
Be careful with locale. It may not work on all systems. For cross-platform code, it is safer to use English format codes directly.
Common Pitfalls and Solutions
One common mistake is using the wrong case for format codes. For example, %Y is different from %y. %Y gives a four-digit year, while %y gives a two-digit year.
Another issue is forgetting to import datetime. Always make sure you have the correct import statement.
If you get a ValueError, check your format string for invalid codes. Python will tell you exactly which code is wrong.
Best Practices
Always use explicit format codes instead of relying on default string representations. The default str() of a datetime object is not always readable.
For APIs and data storage, use ISO 8601 format: %Y-%m-%dT%H:%M:%S. This is a standard that many systems understand.
For user interfaces, use a format like %B %d, %Y or %A, %B %d. This makes the date easy to read.
If you need to handle time zones, consider using the pytz library or Python's built-in zoneinfo module. For more details, check out our Python Datetime Format Guide.
Full Working Example
Here is a complete script that demonstrates several conversions.
from datetime import datetime
# Get current datetime
now = datetime.now()
# Different format styles
iso_format = now.strftime("%Y-%m-%dT%H:%M:%S")
readable_format = now.strftime("%A, %B %d, %Y")
time_only = now.strftime("%I:%M %p")
date_only = now.strftime("%m/%d/%Y")
print("ISO 8601:", iso_format)
print("Readable:", readable_format)
print("Time only:", time_only)
print("Date only:", date_only)
ISO 8601: 2025-03-15T14:30:45
Readable: Saturday, March 15, 2025
Time only: 02:30 PM
Date only: 03/15/2025
This example shows how flexible strftime is. You can create any string format you need.
Conclusion
Converting a Python datetime to a string is simple with strftime. You just need to know the right format codes. Use %Y for year, %m for month, %d for day, and %H:%M:%S for time. Combine them with any text to create readable strings.
Remember to test your format strings. Print the result to make sure it looks correct. For more advanced topics like parsing strings back to datetime, see our Master Python Datetime Guide. If you need to work with the current time frequently, our Python datetime now article is also helpful.
Practice with different formats. Soon you will be able to convert any datetime to a string without looking up the codes.