Common Format Codes Reference Catalog
๐ท๏ธ Working with Dates and Time / Formatting and Parsing Dates
When working with dates and time in Python, you will often need to convert date objects into readable strings or parse strings back into date objects. The strftime (string format time) and strptime (string parse time) methods rely on a set of format codes that tell Python exactly how to interpret or display date and time components. This reference catalog covers the most commonly used format codes that you will encounter in everyday scripting and automation tasks.
โ๏ธ Year Format Codes
These codes control how the year portion of a date is displayed or parsed.
- %Y โ Full year with century (e.g., 2024, 1999)
- %y โ Two-digit year without century (e.g., 24, 99)
- %G โ ISO week-based year (used with week number codes)
- %g โ Two-digit ISO week-based year
๐ Month Format Codes
Use these to represent the month in numeric, abbreviated, or full text form.
- %m โ Month as a zero-padded number (e.g., 01 for January, 12 for December)
- %b โ Abbreviated month name (e.g., Jan, Feb, Dec)
- %B โ Full month name (e.g., January, December)
๐๏ธ Day Format Codes
These codes handle the day of the month and the day of the week.
- %d โ Day of the month as a zero-padded number (e.g., 01, 15, 31)
- %j โ Day of the year as a zero-padded number (e.g., 001 to 366)
- %a โ Abbreviated weekday name (e.g., Mon, Tue, Sun)
- %A โ Full weekday name (e.g., Monday, Tuesday)
- %w โ Weekday as a number (0 for Sunday, 6 for Saturday)
- %u โ Weekday as a number (1 for Monday, 7 for Sunday)
๐ Time Format Codes
For representing hours, minutes, seconds, and time-related components.
- %H โ Hour in 24-hour format, zero-padded (e.g., 00 to 23)
- %I โ Hour in 12-hour format, zero-padded (e.g., 01 to 12)
- %M โ Minute as a zero-padded number (e.g., 00 to 59)
- %S โ Second as a zero-padded number (e.g., 00 to 59)
- %f โ Microsecond as a six-digit zero-padded number (e.g., 000000 to 999999)
- %p โ AM or PM designation (e.g., AM, PM)
๐ Week and Timezone Codes
These codes are useful for ISO week numbering and timezone handling.
- %W โ Week number of the year (Monday as first day of week), zero-padded (e.g., 00 to 53)
- %U โ Week number of the year (Sunday as first day of week), zero-padded (e.g., 00 to 53)
- %V โ ISO week number (Monday as first day), zero-padded (e.g., 01 to 53)
- %z โ UTC offset in the form +HHMM or -HHMM (e.g., +0530, -0400)
- %Z โ Timezone name (e.g., UTC, EST, IST)
๐ ๏ธ Common Format Combinations
Here are some frequently used format patterns that combine multiple codes for practical use.
- %Y-%m-%d โ Standard date format (e.g., 2024-12-25)
- %d/%m/%Y โ Day-first date format (e.g., 25/12/2024)
- %m/%d/%Y โ Month-first date format (e.g., 12/25/2024)
- %Y-%m-%d %H:%M:%S โ Full datetime with 24-hour time (e.g., 2024-12-25 14:30:00)
- %a, %d %b %Y %H:%M:%S %z โ RFC 2822 style (e.g., Wed, 25 Dec 2024 14:30:00 +0000)
- %I:%M:%S %p โ 12-hour time with AM/PM (e.g., 02:30:00 PM)
๐ Quick Reference Comparison Table
| Code | Meaning | Example Output |
|---|---|---|
| %Y | Full year | 2024 |
| %y | Two-digit year | 24 |
| %m | Zero-padded month | 12 |
| %b | Abbreviated month | Dec |
| %B | Full month name | December |
| %d | Zero-padded day | 25 |
| %a | Abbreviated weekday | Wed |
| %A | Full weekday | Wednesday |
| %H | 24-hour hour | 14 |
| %I | 12-hour hour | 02 |
| %M | Minute | 30 |
| %S | Second | 00 |
| %p | AM/PM | PM |
| %z | UTC offset | +0000 |
| %Z | Timezone name | UTC |
๐ต๏ธ Practical Usage Tips
- Always match the format code case carefully. For example, %Y (full year) and %y (two-digit year) produce very different results.
- When parsing strings with strptime, the format string must exactly match the structure of the input string, including any separators like dashes, slashes, or colons.
- For 12-hour time formatting, always include %p to distinguish AM from PM, otherwise the output may be ambiguous.
- The %f code for microseconds is most useful when working with high-precision timestamps or logging events that occur rapidly.
- Timezone codes (%z and %Z) only work correctly if the datetime object has timezone information attached. Naive datetime objects will return empty strings for these codes.
๐งช Example Format Strings in Practice
- To display a timestamp in a log file: %Y-%m-%d %H:%M:%S produces 2024-12-25 14:30:00
- For a user-friendly date on a report: %B %d, %Y produces December 25, 2024
- For parsing a common API date format: %Y-%m-%dT%H:%M:%S%z matches strings like 2024-12-25T14:30:00+0000
- For a compact filename-friendly format: %Y%m%d_%H%M%S produces 20241225_143000
This catalog serves as a quick lookup reference whenever you need to format or parse date and time strings in Python. Bookmark this page and refer back to it as you build scripts that handle scheduling, logging, data processing, or any task involving temporal data.
This reference catalog lists the most common format codes used with strftime() and strptime() to convert between datetime objects and strings.
๐ Example 1: Year, Month, and Day Codes
Shows the three most common codes for extracting year, month, and day components.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 0)
year_full = now.strftime("%Y")
year_short = now.strftime("%y")
month_num = now.strftime("%m")
month_name_full = now.strftime("%B")
month_name_short = now.strftime("%b")
day_num = now.strftime("%d")
day_name_full = now.strftime("%A")
day_name_short = now.strftime("%a")
print(year_full)
print(year_short)
print(month_num)
print(month_name_full)
print(month_name_short)
print(day_num)
print(day_name_full)
print(day_name_short)
๐ค Output: 2024
๐ค Output: 24
๐ค Output: 03
๐ค Output: March
๐ค Output: Mar
๐ค Output: 15
๐ค Output: Friday
๐ค Output: Fri
โฐ Example 2: Time Component Codes
Demonstrates codes for hours, minutes, seconds, and AM/PM markers.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 45)
hour_24 = now.strftime("%H")
hour_12 = now.strftime("%I")
minute = now.strftime("%M")
second = now.strftime("%S")
am_pm = now.strftime("%p")
print(hour_24)
print(hour_12)
print(minute)
print(second)
print(am_pm)
๐ค Output: 14
๐ค Output: 02
๐ค Output: 30
๐ค Output: 45
๐ค Output: PM
๐๏ธ Example 3: Combined Date and Time Format
Shows how to build a full timestamp string using multiple codes together.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 45)
full_date = now.strftime("%A, %B %d, %Y")
full_time = now.strftime("%I:%M:%S %p")
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
print(full_date)
print(full_time)
print(timestamp)
๐ค Output: Friday, March 15, 2024
๐ค Output: 02:30:45 PM
๐ค Output: 2024-03-15 14:30:45
๐ Example 4: Parsing a String into a Datetime Object
Shows how to use format codes in reverse with strptime() to read a date string.
from datetime import datetime
date_string = "2024-03-15 14:30:45"
date_format = "%Y-%m-%d %H:%M:%S"
parsed_date = datetime.strptime(date_string, date_format)
print(parsed_date)
print(parsed_date.year)
print(parsed_date.month)
print(parsed_date.day)
๐ค Output: 2024-03-15 14:30:45
๐ค Output: 2024
๐ค Output: 3
๐ค Output: 15
๐ Example 5: Common Date Formats for Engineers
Shows practical format strings engineers use for logs, reports, and filenames.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 45)
log_format = now.strftime("[%Y-%m-%d %H:%M:%S]")
file_format = now.strftime("report_%Y%m%d_%H%M%S")
iso_format = now.strftime("%Y-%m-%dT%H:%M:%S")
us_format = now.strftime("%m/%d/%Y")
eu_format = now.strftime("%d.%m.%Y")
print(log_format)
print(file_format)
print(iso_format)
print(us_format)
print(eu_format)
๐ค Output: [2024-03-15 14:30:45]
๐ค Output: report_20240315_143045
๐ค Output: 2024-03-15T14:30:45
๐ค Output: 03/15/2024
๐ค Output: 15.03.2024
๐ Common Format Codes Quick Reference Table
| Code | Description | Example Output |
|---|---|---|
%Y |
4-digit year | 2024 |
%y |
2-digit year | 24 |
%m |
Month as zero-padded number | 03 |
%B |
Full month name | March |
%b |
Abbreviated month name | Mar |
%d |
Day of month, zero-padded | 15 |
%A |
Full weekday name | Friday |
%a |
Abbreviated weekday name | Fri |
%H |
Hour (24-hour clock) | 14 |
%I |
Hour (12-hour clock) | 02 |
%M |
Minute, zero-padded | 30 |
%S |
Second, zero-padded | 45 |
%p |
AM or PM | PM |
When working with dates and time in Python, you will often need to convert date objects into readable strings or parse strings back into date objects. The strftime (string format time) and strptime (string parse time) methods rely on a set of format codes that tell Python exactly how to interpret or display date and time components. This reference catalog covers the most commonly used format codes that you will encounter in everyday scripting and automation tasks.
โ๏ธ Year Format Codes
These codes control how the year portion of a date is displayed or parsed.
- %Y โ Full year with century (e.g., 2024, 1999)
- %y โ Two-digit year without century (e.g., 24, 99)
- %G โ ISO week-based year (used with week number codes)
- %g โ Two-digit ISO week-based year
๐ Month Format Codes
Use these to represent the month in numeric, abbreviated, or full text form.
- %m โ Month as a zero-padded number (e.g., 01 for January, 12 for December)
- %b โ Abbreviated month name (e.g., Jan, Feb, Dec)
- %B โ Full month name (e.g., January, December)
๐๏ธ Day Format Codes
These codes handle the day of the month and the day of the week.
- %d โ Day of the month as a zero-padded number (e.g., 01, 15, 31)
- %j โ Day of the year as a zero-padded number (e.g., 001 to 366)
- %a โ Abbreviated weekday name (e.g., Mon, Tue, Sun)
- %A โ Full weekday name (e.g., Monday, Tuesday)
- %w โ Weekday as a number (0 for Sunday, 6 for Saturday)
- %u โ Weekday as a number (1 for Monday, 7 for Sunday)
๐ Time Format Codes
For representing hours, minutes, seconds, and time-related components.
- %H โ Hour in 24-hour format, zero-padded (e.g., 00 to 23)
- %I โ Hour in 12-hour format, zero-padded (e.g., 01 to 12)
- %M โ Minute as a zero-padded number (e.g., 00 to 59)
- %S โ Second as a zero-padded number (e.g., 00 to 59)
- %f โ Microsecond as a six-digit zero-padded number (e.g., 000000 to 999999)
- %p โ AM or PM designation (e.g., AM, PM)
๐ Week and Timezone Codes
These codes are useful for ISO week numbering and timezone handling.
- %W โ Week number of the year (Monday as first day of week), zero-padded (e.g., 00 to 53)
- %U โ Week number of the year (Sunday as first day of week), zero-padded (e.g., 00 to 53)
- %V โ ISO week number (Monday as first day), zero-padded (e.g., 01 to 53)
- %z โ UTC offset in the form +HHMM or -HHMM (e.g., +0530, -0400)
- %Z โ Timezone name (e.g., UTC, EST, IST)
๐ ๏ธ Common Format Combinations
Here are some frequently used format patterns that combine multiple codes for practical use.
- %Y-%m-%d โ Standard date format (e.g., 2024-12-25)
- %d/%m/%Y โ Day-first date format (e.g., 25/12/2024)
- %m/%d/%Y โ Month-first date format (e.g., 12/25/2024)
- %Y-%m-%d %H:%M:%S โ Full datetime with 24-hour time (e.g., 2024-12-25 14:30:00)
- %a, %d %b %Y %H:%M:%S %z โ RFC 2822 style (e.g., Wed, 25 Dec 2024 14:30:00 +0000)
- %I:%M:%S %p โ 12-hour time with AM/PM (e.g., 02:30:00 PM)
๐ Quick Reference Comparison Table
| Code | Meaning | Example Output |
|---|---|---|
| %Y | Full year | 2024 |
| %y | Two-digit year | 24 |
| %m | Zero-padded month | 12 |
| %b | Abbreviated month | Dec |
| %B | Full month name | December |
| %d | Zero-padded day | 25 |
| %a | Abbreviated weekday | Wed |
| %A | Full weekday | Wednesday |
| %H | 24-hour hour | 14 |
| %I | 12-hour hour | 02 |
| %M | Minute | 30 |
| %S | Second | 00 |
| %p | AM/PM | PM |
| %z | UTC offset | +0000 |
| %Z | Timezone name | UTC |
๐ต๏ธ Practical Usage Tips
- Always match the format code case carefully. For example, %Y (full year) and %y (two-digit year) produce very different results.
- When parsing strings with strptime, the format string must exactly match the structure of the input string, including any separators like dashes, slashes, or colons.
- For 12-hour time formatting, always include %p to distinguish AM from PM, otherwise the output may be ambiguous.
- The %f code for microseconds is most useful when working with high-precision timestamps or logging events that occur rapidly.
- Timezone codes (%z and %Z) only work correctly if the datetime object has timezone information attached. Naive datetime objects will return empty strings for these codes.
๐งช Example Format Strings in Practice
- To display a timestamp in a log file: %Y-%m-%d %H:%M:%S produces 2024-12-25 14:30:00
- For a user-friendly date on a report: %B %d, %Y produces December 25, 2024
- For parsing a common API date format: %Y-%m-%dT%H:%M:%S%z matches strings like 2024-12-25T14:30:00+0000
- For a compact filename-friendly format: %Y%m%d_%H%M%S produces 20241225_143000
This catalog serves as a quick lookup reference whenever you need to format or parse date and time strings in Python. Bookmark this page and refer back to it as you build scripts that handle scheduling, logging, data processing, or any task involving temporal data.
Interactive Views
You are currently in ๐ All-in-One mode. Use the tabs at the top to switch to ๐ Theory Only or ๐ป Code Only views.
This reference catalog lists the most common format codes used with strftime() and strptime() to convert between datetime objects and strings.
๐ Example 1: Year, Month, and Day Codes
Shows the three most common codes for extracting year, month, and day components.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 0)
year_full = now.strftime("%Y")
year_short = now.strftime("%y")
month_num = now.strftime("%m")
month_name_full = now.strftime("%B")
month_name_short = now.strftime("%b")
day_num = now.strftime("%d")
day_name_full = now.strftime("%A")
day_name_short = now.strftime("%a")
print(year_full)
print(year_short)
print(month_num)
print(month_name_full)
print(month_name_short)
print(day_num)
print(day_name_full)
print(day_name_short)
๐ค Output: 2024
๐ค Output: 24
๐ค Output: 03
๐ค Output: March
๐ค Output: Mar
๐ค Output: 15
๐ค Output: Friday
๐ค Output: Fri
โฐ Example 2: Time Component Codes
Demonstrates codes for hours, minutes, seconds, and AM/PM markers.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 45)
hour_24 = now.strftime("%H")
hour_12 = now.strftime("%I")
minute = now.strftime("%M")
second = now.strftime("%S")
am_pm = now.strftime("%p")
print(hour_24)
print(hour_12)
print(minute)
print(second)
print(am_pm)
๐ค Output: 14
๐ค Output: 02
๐ค Output: 30
๐ค Output: 45
๐ค Output: PM
๐๏ธ Example 3: Combined Date and Time Format
Shows how to build a full timestamp string using multiple codes together.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 45)
full_date = now.strftime("%A, %B %d, %Y")
full_time = now.strftime("%I:%M:%S %p")
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
print(full_date)
print(full_time)
print(timestamp)
๐ค Output: Friday, March 15, 2024
๐ค Output: 02:30:45 PM
๐ค Output: 2024-03-15 14:30:45
๐ Example 4: Parsing a String into a Datetime Object
Shows how to use format codes in reverse with strptime() to read a date string.
from datetime import datetime
date_string = "2024-03-15 14:30:45"
date_format = "%Y-%m-%d %H:%M:%S"
parsed_date = datetime.strptime(date_string, date_format)
print(parsed_date)
print(parsed_date.year)
print(parsed_date.month)
print(parsed_date.day)
๐ค Output: 2024-03-15 14:30:45
๐ค Output: 2024
๐ค Output: 3
๐ค Output: 15
๐ Example 5: Common Date Formats for Engineers
Shows practical format strings engineers use for logs, reports, and filenames.
from datetime import datetime
now = datetime(2024, 3, 15, 14, 30, 45)
log_format = now.strftime("[%Y-%m-%d %H:%M:%S]")
file_format = now.strftime("report_%Y%m%d_%H%M%S")
iso_format = now.strftime("%Y-%m-%dT%H:%M:%S")
us_format = now.strftime("%m/%d/%Y")
eu_format = now.strftime("%d.%m.%Y")
print(log_format)
print(file_format)
print(iso_format)
print(us_format)
print(eu_format)
๐ค Output: [2024-03-15 14:30:45]
๐ค Output: report_20240315_143045
๐ค Output: 2024-03-15T14:30:45
๐ค Output: 03/15/2024
๐ค Output: 15.03.2024
๐ Common Format Codes Quick Reference Table
| Code | Description | Example Output |
|---|---|---|
%Y |
4-digit year | 2024 |
%y |
2-digit year | 24 |
%m |
Month as zero-padded number | 03 |
%B |
Full month name | March |
%b |
Abbreviated month name | Mar |
%d |
Day of month, zero-padded | 15 |
%A |
Full weekday name | Friday |
%a |
Abbreviated weekday name | Fri |
%H |
Hour (24-hour clock) | 14 |
%I |
Hour (12-hour clock) | 02 |
%M |
Minute, zero-padded | 30 |
%S |
Second, zero-padded | 45 |
%p |
AM or PM | PM |