As a Python developer working on projects across the USA, I came across a scenario where I needed to convert datetime to string when working with date and time data as a part of my project. After researching many ways I found several important ways to achieve this task. In this article, I will help you learn how to convert datetime to string in Python with suitable examples.
Use strftime to Convert Datetime to String in Python
The most common method to convert a datetime object to a string in Python is by using the strftime method. The strftime method allows you to specify the format of the output string.
Read How to Find the Last Occurrence of a Substring in a String Using Python?
Example 1: Format a Date for Display
Let’s say you are building a web application for a travel agency based in New York. You need to display the booking date in a user-friendly format.
from datetime import datetime
# Current date and time
now = datetime.now()
# Format the date as 'Month Day, Year'
formatted_date = now.strftime('%B %d, %Y')
print(f"Booking Date: {formatted_date}")Output:
Booking Date: January 29, 2025I have executed the above example code and added the screenshot below.

Check out How to Iterate Through a String in Python?
Example 2: Log Events with Timestamps
Suppose you are developing a logging system for a tech company in San Francisco. You need to log events with precise timestamps.
from datetime import datetime
# Event timestamp
event_time = datetime.now()
# Format the timestamp as 'YYYY-MM-DD HH:MM:SS'
formatted_timestamp = event_time.strftime('%Y-%m-%d %H:%M:%S')
print(f"Event Logged at: {formatted_timestamp}")Output:
Event Logged at: 2025-01-29 16:26:04I have executed the above example code and added the screenshot below.

Read How to Add Characters to a String in Python?
Common Date and Time Formats
Here are some common formats you might use, along with their strftime codes:
- Full Date and Time:
%Y-%m-%d %H:%M:%S(e.g.,2024-11-11 14:30:45) - Month/Day/Year:
%m/%d/%Y(e.g.,11/11/2024) - Day-Month-Year:
%d-%m-%Y(e.g.,11-11-2024) - Hour: Minute AM/PM:
%I:%M %p(e.g.,02:30 PM)
Check out How to Escape Curly Braces in Python Format Strings?
Handle Different Timezones
When working with applications that serve users across different time zones, it is crucial to handle timezone conversions correctly. Python’s pytz library can help with this.
Example 1: Convert Timezones
Imagine you are working on a project for a multinational company based in Chicago. You need to convert the local time to UTC for consistent logging.
from datetime import datetime
import pytz
# Local time in Chicago
local_time = datetime.now(pytz.timezone('America/Chicago'))
# Convert to UTC
utc_time = local_time.astimezone(pytz.utc)
# Format the UTC time
formatted_utc_time = utc_time.strftime('%Y-%m-%d %H:%M:%S %Z%z')
print(f"UTC Time: {formatted_utc_time}")Output:
UTC Time: 2025-01-29 10:57:27 UTC+0000I have executed the above example code and added the screenshot below.

Check out How to Parse a String in Python?
Advanced Formatting with Custom Functions
For more complex formatting needs, you can create custom functions to handle specific scenarios.
Example 1: Custom Date Formatting Function
Let’s say you need to format dates in a specific way for a financial report in Los Angeles.
from datetime import datetime
def custom_date_format(date_obj):
return date_obj.strftime('%A, %d %B %Y')
# Example date
report_date = datetime(2024, 11, 11)
# Apply custom format
formatted_report_date = custom_date_format(report_date)
print(f"Report Date: {formatted_report_date}")The output will be Report Date: Monday, 11 November 2024.
Read How to Add Line Breaks in Python Strings?
Conclusion
In this tutorial, we have discussed how to convert datetime to string in Python. I discussed strftime() method to format date and log events with timestamps. I also covered common date and time formats, how to handle timezones with examples, and advanced formatting with custom functions.
You may also like to read:
- How to Count Words in a String Using Python?
- How to Continue Long Strings on the Next Line in Python?
- How to Remove Commas from a String in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.