Of course! "Python dateformatter" usually refers to the process of converting Python's datetime objects into formatted strings. The primary tool for this is the strftime() method, which stands for "string format time".

Here's a comprehensive guide to formatting dates in Python, from the basics to more advanced scenarios.
The Basics: strftime() and strptime()
strftime() (String Format Time)
This is the method you'll use most often. You call it on a datetime object, and it returns a formatted string based on a set of format codes.
Format Codes: These are special characters (like %Y, %m, %d) that represent parts of the date and time.
| Code | Meaning | Example |
|---|---|---|
%Y |
Year with century as a decimal number. | 2025 |
%y |
Year without century as a zero-padded decimal number. | 23 |
%m |
Month as a zero-padded decimal number. | 09 (September) |
%B |
Month as full locale name. | September |
%b |
Month as abbreviated locale name. | Sep |
%d |
Day of the month as a zero-padded decimal number. | 05 |
%A |
Weekday as full locale name. | Monday |
%a |
Weekday as abbreviated locale name. | Mon |
%H |
Hour (24-hour clock) as a zero-padded decimal number. | 14 |
%I |
Hour (12-hour clock) as a zero-padded decimal number. | 02 |
%M |
Minute as a zero-padded decimal number. | 05 |
%S |
Second as a zero-padded decimal number. | 09 |
%p |
Locale’s equivalent of either AM or PM. | PM |
%f |
Microsecond as a decimal number, zero-padded on the left. | 000123 |
%Z |
Timezone name (if available). | EST, UTC |
%z |
UTC offset in the form ±HHMM[SS[.ffffff]] (empty if naive). | +0000 |
| A literal '%' character. |
strptime() (String Parse Time)
This is the opposite of strftime(). It takes a string and a format code, and parses it into a datetime object.

Simple Examples of strftime()
Let's start by creating a datetime object and formatting it in different ways.
import datetime
# Get the current date and time
now = datetime.datetime.now()
print(f"Original datetime object: {now}\n")
# --- Basic Formatting ---
# Format: YYYY-MM-DD HH:MM:SS
# This is the ISO 8601 format, very common
iso_format = now.strftime("%Y-%m-%d %H:%M:%S")
print(f"ISO Format: {iso_format}")
# Format: MM/DD/YYYY
us_date_format = now.strftime("%m/%d/%Y")
print(f"US Date Format: {us_date_format}")
# Format: DD-MM-YYYY
eu_date_format = now.strftime("%d-%m-%Y")
print(f"EU Date Format: {eu_date_format}")
# --- More Readable Formats ---
# Format: Month Day, Year
readable_date = now.strftime("%B %d, %Y")
print(f"Readable Date: {readable_date}")
# Format: Day of the week, Month Day, Year
full_long_date = now.strftime("%A, %B %d, %Y")
print(f"Full Long Date: {full_long_date}")
# --- Including Time ---
# Format: HH:MM:AM/PM
twelve_hour_time = now.strftime("%I:%M %p")
print(f"12-Hour Time: {twelve_hour_time}")
# Format: Year-Month-Day Hour:Minute:Second
file_system_timestamp = now.strftime("%Y-%m-%d_%H-%M-%S")
print(f"File System Timestamp: {file_system_timestamp}")
Example Output (will vary based on when you run it):
Original datetime object: 2025-10-27 10:30:55.123456
ISO Format: 2025-10-27 10:30:55
US Date Format: 10/27/2025
EU Date Format: 27-10-2025
Readable Date: October 27, 2025
Full Long Date: Friday, October 27, 2025
12-Hour Time: 10:30 AM
File System Timestamp: 2025-10-27_10-30-55
Parsing Dates with strptime()
If you have a date string and need to convert it into a datetime object for calculations or manipulation, use strptime().
import datetime
# A date string in a specific format
date_string = "October 27, 2025"
# Define the format code that matches the string
format_code = "%B %d, %Y"
# Parse the string into a datetime object
parsed_date = datetime.datetime.strptime(date_string, format_code)
print(f"Original String: '{date_string}'")
print(f"Parsed Datetime: {parsed_date}")
print(f"Type of parsed object: {type(parsed_date)}")
# Now you can perform operations on the datetime object
future_date = parsed_date + datetime.timedelta(days=30)
print(f"Date 30 days later: {future_date.strftime('%B %d, %Y')}")
Output:

Original String: 'October 27, 2025'
Parsed Datetime: 2025-10-27 00:00:00
Type of parsed object: <class 'datetime.datetime'>
Date 30 days later: November 26, 2025
Handling Timezones
Working with timezones is crucial for robust applications. Python's zoneinfo module (standard in Python 3.9+) is the modern way to handle this.
import datetime
from zoneinfo import ZoneInfo # Use 'import pytz' for older Python versions
# 1. Create a timezone-aware datetime object
# Let's get the current time in New York
ny_tz = ZoneInfo("America/New_York")
now_in_ny = datetime.datetime.now(ny_tz)
print(f"Time in New York: {now_in_ny}")
print(f"Timezone info: {now_in_ny.tzinfo}")
# 2. Format a timezone-aware datetime object
# The %z code will include the UTC offset
ny_with_offset = now_in_ny.strftime("%Y-%m-%d %H:%M:%S %Z%z")
print(f"NY Time with Offset: {ny_with_offset}")
# 3. Convert between timezones
# Get the current time in London
london_tz = ZoneInfo("Europe/London")
now_in_london = now_in_ny.astimezone(london_tz)
print(f"\nTime in London: {now_in_london}")
print(f"London Time with Offset: {now_in_london.strftime('%Y-%m-%d %H:%M:%S %Z%z')}")
Output:
Time in New York: 2025-10-27 10:30:55.123456-04:00
Timezone info: America/New_York
NY Time with Offset: 2025-10-27 10:30:55 EDT-0400
Time in London: 2025-10-27 15:30:55.123456+01:00
London Time with Offset: 2025-10-27 15:30:55 BST+0100
Common Pitfalls and Best Practices
-
Mismatched Format Codes:
strptime()will raise aValueErrorif the format code does not match the string.# This will fail! # datetime.datetime.strptime("27-10-2025", "%m/%d/%Y") # ValueError: time data '27-10-2025' does not match format '%m/%d/%Y' -
Naive vs. Aware Datetimes: A "naive"
datetimeobject has no timezone information. An "aware" one does. It's a best practice to always use aware datetimes, especially when dealing with data that might cross timezones or be stored in a database. -
Locale-Specific Formatting: Codes like
%B(full month name) and%A(full weekday name) are locale-dependent. If your system's locale is not English, you might get different results. For consistent, non-locale-dependent formatting, stick to codes like%m,%d,%Y. -
For Simpler Cases:
date.isoformat()If you just need a standard, unambiguous date string, thedateobject has a handyisoformat()method.import
