Last modified: May 04, 2026 By Alexander Williams

Python datetime strptime: Parse Strings to Dates

Working with dates in Python is a common task. You often receive dates as strings from user input, files, or APIs. To perform calculations or comparisons, you need to convert these strings into datetime objects. This is where strptime comes in. It stands for "string parse time". It is a powerful method that turns a date string into a structured datetime object.

This guide will teach you everything about strptime. You will learn the syntax, format codes, and common use cases. We will also cover common errors and how to avoid them. By the end, you will be comfortable parsing any date string in Python.

What is strptime?

strptime is a method of the datetime class. It belongs to the datetime module. It takes two main arguments: the date string and the format string. The format string tells Python how to read the date string. It uses special format codes to represent different parts of a date and time.

The method returns a datetime object. This object has attributes like year, month, day, hour, minute, and second. You can then use these attributes for calculations, formatting, or storage.

Basic Syntax of strptime

The syntax is straightforward. You call datetime.strptime(date_string, format). The first parameter is the string you want to parse. The second parameter is a string that describes the format of the date string.

Here is a simple example:

from datetime import datetime

# A date string in a common format
date_str = "2023-10-15"

# Parse it using the format code for year-month-day
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
print(date_obj)  # Output: 2023-10-15 00:00:00

The output shows a datetime object. The time part defaults to midnight because we did not include time information in the string.

Common Format Codes for strptime

To use strptime correctly, you must know the format codes. These codes are case-sensitive. Here are the most important ones:

  • %Y - Four-digit year (e.g., 2023)
  • %m - Two-digit month (01 to 12)
  • %d - Two-digit day (01 to 31)
  • %H - Two-digit hour (00 to 23)
  • %M - Two-digit minute (00 to 59)
  • %S - Two-digit second (00 to 59)
  • %B - Full month name (e.g., January)
  • %b - Abbreviated month name (e.g., Jan)
  • %A - Full weekday name (e.g., Monday)
  • %a - Abbreviated weekday name (e.g., Mon)
  • %p - AM or PM

You combine these codes to match your date string exactly. Any character in the format string that is not a format code must match the string literally. For example, dashes, slashes, or spaces must be present in both.

Parsing Different Date Formats

Let's look at several examples of parsing different date string formats. This will help you understand how to adapt strptime to your needs.

Example 1: US Date Format (Month/Day/Year)

from datetime import datetime

date_str = "10/15/2023"
date_obj = datetime.strptime(date_str, "%m/%d/%Y")
print(date_obj)
2023-10-15 00:00:00

Example 2: Date with Full Month Name

from datetime import datetime

date_str = "October 15, 2023"
date_obj = datetime.strptime(date_str, "%B %d, %Y")
print(date_obj)
2023-10-15 00:00:00

Example 3: Date and Time with AM/PM

from datetime import datetime

date_str = "15-10-2023 02:30 PM"
date_obj = datetime.strptime(date_str, "%d-%m-%Y %I:%M %p")
print(date_obj)
2023-10-15 14:30:00

Notice that %I is used for 12-hour clock, and %p handles AM/PM. The output shows 14:30, which is 2:30 PM in 24-hour format.

Common Errors and How to Fix Them

Beginners often encounter errors with strptime. The most common is the ValueError. This happens when the format string does not match the date string exactly.

Error: Format Mismatch

from datetime import datetime

date_str = "2023-10-15"
# Wrong format: using slashes instead of dashes
date_obj = datetime.strptime(date_str, "%Y/%m/%d")

This will raise a ValueError because the string uses dashes, but the format specifies slashes. Always ensure the literal characters match.

Error: Wrong Case in Month Name

from datetime import datetime

date_str = "october 15, 2023"
# 'october' is lowercase, but %B expects title case
date_obj = datetime.strptime(date_str, "%B %d, %Y")

This will also raise a ValueError. Month names are case-sensitive. Use "October" with a capital 'O'.

Best Practices for Using strptime

Follow these tips to write robust code with strptime.

  • Always test your format string with a sample date string before using it in production.
  • Use try-except blocks to catch ValueError when parsing user input.
  • Be careful with time zones. strptime does not handle time zones directly. You may need to use third-party libraries like pytz.
  • Standardize your date strings if possible. The ISO 8601 format (%Y-%m-%d) is widely recommended.

Real-World Use Cases

strptime is essential in many applications. For example, you might parse dates from a CSV file or a web form. It is also useful when working with logs that contain timestamps. Once you have a datetime object, you can use it to filter data, calculate durations, or convert to another format using strftime. For more on formatting, see our Python Datetime Format Guide.

Another common task is getting the current date and time. You can then parse a string and compare it with the current moment. Check our guide on Python datetime now: Get Current Date & Time for more details.

Conclusion

Python's strptime is a vital tool for any developer working with dates. It converts string representations of dates into flexible datetime objects. By mastering the format codes and understanding common pitfalls, you can handle any date parsing task with confidence. Always match your format string exactly to the date string. Use error handling to manage unexpected input. With practice, parsing dates will become second nature. For a complete overview of datetime in Python, visit our Master Python Datetime Guide. Start using strptime today to make your date processing clean and efficient.