Last modified: May 06, 2026 By Alexander Williams

Python Datetime Parse: A Simple Guide

Working with dates and times is a common task in Python. You often get date information as a string. To use it in calculations, you need to convert it. This process is called parsing.

Python’s datetime module provides powerful tools for this. The key function is strptime. It stands for "string parse time". It turns a string into a datetime object.

This guide will teach you how to parse dates in Python. We will use simple examples. You will learn the format codes. You will also see how to handle common errors.

What is strptime?

strptime is a method in the datetime class. It takes two arguments. The first is the date string. The second is the format string. The format string tells Python how the date is structured.

The syntax is simple:


from datetime import datetime

date_string = "2023-10-05"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
print(date_object)

In this example, %Y stands for a four-digit year. %m is a two-digit month. %d is a two-digit day. The dashes in the format string must match the string.

The output is a datetime object:


2023-10-05 00:00:00

The time defaults to midnight. This is because we did not provide time information.

Common Format Codes

You need to know the format codes. They are the heart of parsing. Here are the most common ones:

  • %Y - Year with century (e.g., 2023)
  • %m - Month as a zero-padded number (01-12)
  • %d - Day of the month as a zero-padded number (01-31)
  • %H - Hour in 24-hour format (00-23)
  • %M - Minute (00-59)
  • %S - Second (00-59)
  • %b - Abbreviated month name (e.g., Jan, Feb)
  • %B - Full month name (e.g., January)
  • %A - Full weekday name (e.g., Monday)

You can combine these codes. The format string must match the input string exactly. A single wrong character will cause an error.

Parsing Different Date Formats

Dates come in many formats. Let’s look at a few examples.

Example 1: European Format

Many countries use day-month-year. You can parse it like this:


from datetime import datetime

date_string = "05-10-2023"  # 5th October 2023
date_object = datetime.strptime(date_string, "%d-%m-%Y")
print(date_object)

Output:


2023-10-05 00:00:00

Example 2: Month Name Format

You might see dates like "October 5, 2023". Use %B for the full month name.


from datetime import datetime

date_string = "October 5, 2023"
date_object = datetime.strptime(date_string, "%B %d, %Y")
print(date_object)

Output:


2023-10-05 00:00:00

Notice the comma in the format string. It matches the comma in the input.

Example 3: Date and Time

Parsing a string with both date and time is easy. Just add the time codes.


from datetime import datetime

date_string = "2023-10-05 14:30:00"
date_object = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
print(date_object)

Output:


2023-10-05 14:30:00

Handling Errors

Parsing fails if the format does not match. Python raises a ValueError. For example, if you try to parse "05/10/2023" with the format "%Y-%m-%d", you get an error.

Always check your format string. A common mistake is using %Y for a two-digit year. Use %y for a two-digit year instead.

Here is an example of a mismatch:


from datetime import datetime

date_string = "05/10/2023"
try:
    date_object = datetime.strptime(date_string, "%Y-%m-%d")
except ValueError as e:
    print(f"Error: {e}")

Output:


Error: time data '05/10/2023' does not match format '%Y-%m-%d'

Use a try-except block to handle such errors gracefully.

Parsing ISO Format Dates

ISO 8601 is a standard date format. It looks like "2023-10-05T14:30:00". You can parse it with strptime. But Python also has a built-in method for this.

The datetime.fromisoformat method is simpler for ISO strings. It does not need a format string.


from datetime import datetime

iso_string = "2023-10-05T14:30:00"
date_object = datetime.fromisoformat(iso_string)
print(date_object)

Output:


2023-10-05 14:30:00

For more details on this, check out our Python Datetime ISOFormat Guide.

Parsing with Timezone Information

Timezones add complexity. The strptime method does not handle timezones well. For robust timezone parsing, you need the dateutil library. But Python 3.7+ has some support.

You can parse a UTC offset like "+0000" using %z.


from datetime import datetime

date_string = "2023-10-05 14:30:00+0000"
date_object = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S%z")
print(date_object)

Output:


2023-10-05 14:30:00+00:00

For a deeper dive into timezones, read our Python Datetime Timezone Guide.

Converting Strings to Timestamps

Sometimes you need a timestamp. A timestamp is the number of seconds since January 1, 1970. You can get it from a parsed datetime object.


from datetime import datetime

date_string = "2023-10-05"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
timestamp = date_object.timestamp()
print(timestamp)

Output:


1696464000.0

Learn more about this in our Python Datetime Timestamp Explained article.

Best Practices for Parsing

Here are some tips to avoid errors:

  • Always use the correct format codes. Test your format string with sample data.
  • Use try-except blocks. This prevents your program from crashing on bad input.
  • Be careful with locale. Month names depend on the system locale.
  • Consider using dateutil.parser for complex strings. It can guess the format automatically.

Conclusion

Parsing dates in Python is essential. The strptime method is your main tool. It is powerful but requires exact format strings.

Remember the common format codes. Handle errors with try-except. For ISO and timezone formats, use specialized methods.

With practice, you will parse dates like a pro. This skill is useful for data analysis, web scraping, and many other tasks. Keep coding and experimenting.