Python strptime & strftime Cheat Sheet (2025 Format Guide)

Python strptime & strftime Cheat Sheet

I think It happens to every Python developer at least once a week.

You are parsing a CSV file. The date column looks like Dec 05, 2023. You type out datetime.strptime(date_str, "%Y-%m-%d") and hit run.

CRASH.

ValueError: time data 'Dec 05, 2023' does not match format '%Y-%m-%d'

You stare at the screen. “Is December %b or %B? Is the day %d or %D?”

Instead of guessing and re-running your script ten times, bookmark this guide. We have compiled the complete, definitive cheat sheet for Python’s date formatting directives, plus the common pitfalls that even senior data scientists forget.

The “Mega-Table” of Directives

Use this table to build your format strings.

DirectiveMeaningExample Output
Year
%YYear with century2025
%yYear without century (zero-padded)25
Month
%mMonth as a decimal number (01-12)12
%BMonth name (Full)December
%bMonth name (Abbreviated)Dec
Day
%dDay of the month (zero-padded)07
%jDay of the year (001-366)341
Weekday
%AWeekday name (Full)Sunday
%aWeekday name (Abbreviated)Sun
%wWeekday as a decimal number (0=Sunday)0
Time
%HHour (24-hour clock)14
%IHour (12-hour clock)02
%pAM or PMPM
%MMinute30
%SSecond05
%fMicrosecond (zero-padded)000000
Timezone
%zUTC offset+0530
%ZTime zone nameIST
The complete Python date formatting reference guide.
Table of Python datetime format directives including year, month, day, and time codes.
Table of Python datetime format directives including year, month, day, and time codes.

strptime vs strftime: What’s the Difference?

The codes are the same, but the direction is different.

  • strptime (String Parse Time): Converts a StringDatetime Object.
    • Use case: Reading data from logs, CSVs, or user input.
    • dt = datetime.strptime("2025-12-07", "%Y-%m-%d")
  • strftime (String Format Time): Converts a Datetime ObjectString.
    • Use case: Printing dates for reports or filenames.
    • s = dt.strftime("%B %d, %Y") # “December 07, 2025”

3 Common Pitfalls That Break Your Code

1. The “Zero Padding” Trap

Python’s strptime is strict. If you use %d (zero-padded day), it expects 07. If your input string is 7 (single digit), it might fail on some platforms or implementations.

  • Fix: Ensure your data is cleaned, or use a custom parser if inputs vary between 7 and 07.

2. The “Locale” Nightmare

If your server is set to en_US, parsing 05/12/2025 usually means May 12th. If your server is set to fr_FR or en_GB, parsing 05/12/2025 means December 5th.

If you use %b (Month name), it depends on the system locale!

  • %b parses “Dec” in English.
  • %b fails to parse “Dec” in French (it expects “déc.”).

Pro Tip: Always set the locale explicitly in your Docker containers or environment if you rely on month names.

3. Case Sensitivity

%m is Month. %M is Minute. %y is Year (2 digits). %Y is Year (4 digits). Mixing these up is the #1 cause of logic bugs where your code runs but produces nonsense data (e.g., your “minutes” become your “month”).

Stop Memorizing the Table

Why waste brain power memorizing %p vs %P?

We built a tool that does the mapping for you. You just paste the date you have, and we generate the Python code.

Try the Python Date Parser Generator

  1. Paste your weird date: Sunday, Dec 7th '25 at 2:00 PM
  2. Select Python: We support datetime and pandas.
  3. Copy the Code: We give you the exact string: "%A, %b %dS '%y at %I:%M %p"

It handles the tricky stuff (like punctuation and spacing) automatically.

Frequently Asked Questions (FAQ)

Q: How do I handle “st”, “nd”, “rd”, “th” in dates (e.g., Dec 7th)? A: Python’s standard strptime does not support ordinal suffixes natively. You cannot use %dth. You must either preprocess the string to remove the suffix or use a third-party library like dateutil.

Q: What is the difference between %y and %Y? A: %Y is for a 4-digit year (2025). %y is for a 2-digit year (25). Be careful with %y—Python has to guess the century (1900s vs 2000s) based on platform rules, which can be risky for historical data.

Q: Why do I get ValueError: time data does not match format? A: This means your format string doesn’t exactly match the input. Look for missing spaces, incorrect separators (using - instead of /), or case-sensitivity errors (%m vs %M).

Q: Can I use this for Pandas? A: Yes! The format codes for pandas.to_datetime(format=...) are exactly the same as the standard Python library.

Bookmark this page (or just use our Generator Tool) and never get a ValueError again.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top