Back to Articles

Python Date Formatting: A Practical Guide

Working with dates is a common task in Python programming. Whether you're building a web application, processing data, or generating reports, you'll often need to format dates in specific ways. This guide covers the most common date formatting patterns with practical examples you can use in your projects. If you need a quick way to format dates without writing code, try this Date & Time Formatter tool.

Getting Started with Python's datetime Module 

First, let's import the necessary module datetime and create a sample date to work with throughout our examples:

from datetime import datetime

# Create a sample date for our examples
sample_date = datetime(2025, 12, 1, 19, 30, 16)
print(f"Original date: {sample_date}")

Code result as follows:

Python Date Formatting: A Practical Guide

Common Date Formatting Patterns

Python uses the strftime() method to format dates. The method takes a format string with special codes that represent different parts of the date. Here are the most useful formatting patterns:

Full Date with Day Name

Format: dddd, MMMM D, YYYY, h:mm:ss a
Example: Sunday, December 1, 2025, 7:30:16 pm
formatted = sample_date.strftime('%A, %B %d, %Y, %I:%M:%S %p')
print(formatted)

Month and Day with Year

Format: MMMM D, YYYY
Example: December 1, 2025
formatted = sample_date.strftime('%B %d, %Y')
print(formatted)

Month with Ordinal Day

Format: MMMM Do YYYY
Example: December 1st 2025

Note: Python's standard library doesn't directly support ordinal indicators (st, nd, rd, th). We need a helper function:

 from datetime import datetime
def get_ordinal(n):
    if 10 <= n % 100 <= 20:
        suffix = 'th'
    else:
        suffix = {1: 'st', 2: 'nd', 3: 'rd'}.get(n % 10, 'th')
    return f"{n}{suffix}"

sample_date = datetime(2025, 12, 1, 19, 30, 16)
day_with_ordinal = get_ordinal(sample_date.day)
formatted = f"{sample_date.strftime('%B')} {day_with_ordinal} {sample_date.year}"
print(formatted)
Output 

Python Date Formatting: A Practical Guide

Day First with Month and Year

Format: Do MMMM YYYY
Example: 1st December 2025
day_with_ordinal = get_ordinal(sample_date.day)
formatted = f"{day_with_ordinal} {sample_date.strftime('%B')} {sample_date.year}"
print(formatted)

Full Date with Time (Ordinal Day)

Format: MMMM Do YYYY, h:mm:ss a
Example: December 1st 2025, 7:30:16 pm
day_with_ordinal = get_ordinal(sample_date.day)
formatted = f"{sample_date.strftime('%B')} {day_with_ordinal} {sample_date.year}, {sample_date.strftime('%I:%M:%S %p').lstrip('0')}"
print(formatted)

Day Name First Format

Format: dddd, D MMMM YYYY, h:mm:ss a
Example: Sunday, 1 December 2025, 7:30:16 pm
formatted = sample_date.strftime('%A, %d %B %Y, %I:%M:%S %p')
print(formatted)

Compact Format with 24-Hour Time

Format: ddd, D MMM YYYY HH:MM:SS A
Example: Sun, 1 Dec 2025 19:30:16 PM
formatted = sample_date.strftime('%a, %d %b %Y %H:%M:%S PM')
print(formatted)

Numeric Formats

Format: MM/DD/YYYY
Example: 12/01/2025
formatted = sample_date.strftime('%m/%d/%Y')
print(formatted)
Format: YYYY-MM-DD
Example: 2025-12-01
formatted = sample_date.strftime('%Y-%m-%d')
print(formatted)

Abbreviated Month Format

Format: MMM D, YYYY
Example: Dec 1, 2025
formatted = sample_date.strftime('%b %d, %Y')
print(formatted)

ISO Format with Timezone

Format: YYYY-MM-DDTHH:mm:ssZ
Example: 2025-12-01T19:30:16+08:00
# For timezone-aware formatting, we need to use timezone information
from datetime import timezone, timedelta

# Create a timezone-aware datetime (UTC+8 as an example)
tz = timezone(timedelta(hours=8))
aware_date = sample_date.replace(tzinfo=tz)
formatted = aware_date.isoformat()
print(formatted)

Complete Reference of Format Codes

Here's a quick reference of the most commonly used format codes in Python's strftime method:

%a - Weekday as abbreviated name (Sun, Mon, etc.)
%A - Weekday as full name (Sunday, Monday, etc.)
%d - Day of the month as zero-padded decimal (01 to 31)
%m - Month as zero-padded decimal (01 to 12)
%b - Month as abbreviated name (Jan, Feb, etc.)
%B - Month as full name (January, February, etc.)
%y - Year without century as zero-padded decimal (00 to 99)
%Y - Year with century as decimal (2025)
%H - Hour (24-hour clock) as zero-padded decimal (00 to 23)
%I - Hour (12-hour clock) as zero-padded decimal (01 to 12)
%p - Locale's equivalent of either AM or PM
%M - Minute as zero-padded decimal (00 to 59)
%S - Second as zero-padded decimal (00 to 59)

Putting It All Together

Here's a complete example that demonstrates multiple formatting options in one script:

from datetime import datetime

def format_date_examples():
    # Create our sample date
    date_obj = datetime(2025, 12, 1, 19, 30, 16)
    
    # Define various format patterns
    formats = [
        ('%A, %B %d, %Y, %I:%M:%S %p', 'Full date with day name'),
        ('%B %d, %Y', 'Month and day with year'),
        ('%m/%d/%Y', 'Numeric US format'),
        ('%Y-%m-%d', 'ISO date format'),
        ('%b %d, %Y', 'Abbreviated month format'),
        ('%a, %d %b %Y %H:%M:%S', 'Compact format with 24-hour time')
    ]
    
    # Apply each format and print results
    for fmt, description in formats:
        formatted = date_obj.strftime(fmt)
        print(f"{description}: {formatted}")

if __name__ == "__main__":
    format_date_examples()

Output

Python Date Formatting: A Practical Guide

Conclusion

Python's datetime module provides powerful tools for working with dates and times. The strftime method is particularly useful for formatting dates according to specific patterns. With the examples in this guide, you should be able to handle most date formatting requirements in your Python projects.

Remember that date formatting can be locale-specific. If you're building applications for international audiences, you might need to consider localization aspects beyond what's covered here.