Parsing Strings into Datetime via strptime

๐Ÿท๏ธ Working with Dates and Time / Formatting and Parsing Dates

๐ŸŽฏ Context Introduction

When working with logs, configuration files, or API responses, dates and times often arrive as plain text strings. For example, a log entry might contain "2024-01-15 14:30:00" or "Jan 15, 2024 2:30 PM". To perform calculations, comparisons, or formatting changes, you need to convert these strings into Python's datetime objects. The strptime method (string parse time) is the tool that makes this conversion possible.


โš™๏ธ What is strptime?

strptime stands for "string parse time". It is a method available on the datetime class that takes a string and a format code, then returns a datetime object.

  • Input: A date/time string (e.g., "2024-03-10")
  • Format: A pattern describing the string's structure (e.g., "%Y-%m-%d")
  • Output: A datetime object you can work with programmatically

The basic syntax looks like this:

from datetime import datetime

datetime.strptime("2024-03-10", "%Y-%m-%d")


๐Ÿ•ต๏ธ Common Format Codes

Format codes tell Python exactly how to interpret each part of your date string. Here are the most frequently used codes:

Code Meaning Example
%Y 4-digit year 2024
%m 2-digit month 03
%d 2-digit day 10
%H 24-hour hour 14
%M Minute 30
%S Second 45
%I 12-hour hour 02
%p AM or PM PM
%b Abbreviated month name Jan
%B Full month name January
%A Full weekday name Monday
%a Abbreviated weekday Mon

๐Ÿ› ๏ธ Practical Examples

Example 1: Standard Date Format

If you have a string like "2024-03-10", you parse it with:

parsed_date = datetime.strptime("2024-03-10", "%Y-%m-%d")

The result is a datetime object representing March 10, 2024 at midnight (default time).

Example 2: Date and Time Together

For a string like "2024-03-10 14:30:00", use:

parsed_datetime = datetime.strptime("2024-03-10 14:30:00", "%Y-%m-%d %H:%M:%S")

This gives you a full datetime object with both date and time.

Example 3: Month Names and 12-Hour Clock

For a string like "Jan 15, 2024 2:30 PM", use:

parsed_datetime = datetime.strptime("Jan 15, 2024 2:30 PM", "%b %d, %Y %I:%M %p")

Notice how the format string mirrors the exact structure of the input, including commas and spaces.


๐Ÿ“Š Common Pitfalls to Avoid

  • Mismatched format codes: If your format string doesn't match the input string exactly, Python raises a ValueError. For example, using %Y for a two-digit year like "24" will fail.
  • Missing leading zeros: The format %m expects "03" not "3". For single-digit months or days without leading zeros, use %-m or %-d (on some systems) or pad the input manually.
  • Case sensitivity: Month names must match exactly. "Jan" works with %b, but "JAN" does not.
  • Time zone awareness: Basic strptime does not handle time zones. For time zone strings like "2024-03-10 14:30:00 UTC", you need additional handling.

๐Ÿงช Testing Your Parsing

After parsing, you can verify the result by checking the object's attributes:

  • parsed_date.year returns the year (e.g., 2024)
  • parsed_date.month returns the month (e.g., 3)
  • parsed_date.day returns the day (e.g., 10)
  • parsed_date.hour returns the hour (e.g., 14)
  • parsed_date.weekday() returns the day of week (Monday=0)

This allows you to confirm that your format string correctly interpreted the input.


๐Ÿ’ก Quick Reference Workflow

  1. Identify the exact format of your date string
  2. Choose the appropriate format codes for each component
  3. Call datetime.strptime(your_string, your_format)
  4. Store the result as a datetime object
  5. Use the object for comparisons, arithmetic, or reformatting

Remember: The format string must match the input string character by character, including spaces, commas, and punctuation. When in doubt, test with a single example before processing large datasets.


The strptime method converts a string representation of a date/time into a Python datetime object using a specified format pattern.


๐Ÿ“˜ Example 1: Parsing a Simple Date String

This example converts a date string in "YYYY-MM-DD" format into a datetime object.

from datetime import datetime

date_string = "2024-12-25"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
print(date_object)

๐Ÿ“ค Output: 2024-12-25 00:00:00


๐Ÿ“˜ Example 2: Parsing a Date with Month Name

This example converts a date string that uses a full month name into a datetime object.

from datetime import datetime

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

๐Ÿ“ค Output: 2023-03-15 00:00:00


๐Ÿ“˜ Example 3: Parsing Date and Time Together

This example converts a string containing both date and time components into a datetime object.

from datetime import datetime

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

๐Ÿ“ค Output: 2024-01-10 14:30:00


๐Ÿ“˜ Example 4: Parsing a 12-Hour Time with AM/PM

This example converts a string with a 12-hour clock and AM/PM indicator into a datetime object.

from datetime import datetime

date_string = "07/25/2024 09:45 PM"
date_object = datetime.strptime(date_string, "%m/%d/%Y %I:%M %p")
print(date_object)

๐Ÿ“ค Output: 2024-07-25 21:45:00


๐Ÿ“˜ Example 5: Parsing a Log Timestamp with Timezone Offset

This example converts a common log format timestamp with timezone offset into a datetime object.

from datetime import datetime

date_string = "2024-11-03T08:15:30+0000"
date_object = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S%z")
print(date_object)

๐Ÿ“ค Output: 2024-11-03 08:15:30+00:00


๐Ÿ“Š Common strptime Format Codes

Format Code Meaning Example
%Y 4-digit year 2024
%m 2-digit month (01-12) 03
%d 2-digit day (01-31) 15
%H 24-hour hour (00-23) 14
%I 12-hour hour (01-12) 09
%M 2-digit minute (00-59) 30
%S 2-digit second (00-59) 45
%p AM or PM AM
%B Full month name March
%b Abbreviated month name Mar
%z Timezone offset (+HHMM) +0000

๐ŸŽฏ Context Introduction

When working with logs, configuration files, or API responses, dates and times often arrive as plain text strings. For example, a log entry might contain "2024-01-15 14:30:00" or "Jan 15, 2024 2:30 PM". To perform calculations, comparisons, or formatting changes, you need to convert these strings into Python's datetime objects. The strptime method (string parse time) is the tool that makes this conversion possible.


โš™๏ธ What is strptime?

strptime stands for "string parse time". It is a method available on the datetime class that takes a string and a format code, then returns a datetime object.

  • Input: A date/time string (e.g., "2024-03-10")
  • Format: A pattern describing the string's structure (e.g., "%Y-%m-%d")
  • Output: A datetime object you can work with programmatically

The basic syntax looks like this:

from datetime import datetime

datetime.strptime("2024-03-10", "%Y-%m-%d")


๐Ÿ•ต๏ธ Common Format Codes

Format codes tell Python exactly how to interpret each part of your date string. Here are the most frequently used codes:

Code Meaning Example
%Y 4-digit year 2024
%m 2-digit month 03
%d 2-digit day 10
%H 24-hour hour 14
%M Minute 30
%S Second 45
%I 12-hour hour 02
%p AM or PM PM
%b Abbreviated month name Jan
%B Full month name January
%A Full weekday name Monday
%a Abbreviated weekday Mon

๐Ÿ› ๏ธ Practical Examples

Example 1: Standard Date Format

If you have a string like "2024-03-10", you parse it with:

parsed_date = datetime.strptime("2024-03-10", "%Y-%m-%d")

The result is a datetime object representing March 10, 2024 at midnight (default time).

Example 2: Date and Time Together

For a string like "2024-03-10 14:30:00", use:

parsed_datetime = datetime.strptime("2024-03-10 14:30:00", "%Y-%m-%d %H:%M:%S")

This gives you a full datetime object with both date and time.

Example 3: Month Names and 12-Hour Clock

For a string like "Jan 15, 2024 2:30 PM", use:

parsed_datetime = datetime.strptime("Jan 15, 2024 2:30 PM", "%b %d, %Y %I:%M %p")

Notice how the format string mirrors the exact structure of the input, including commas and spaces.


๐Ÿ“Š Common Pitfalls to Avoid

  • Mismatched format codes: If your format string doesn't match the input string exactly, Python raises a ValueError. For example, using %Y for a two-digit year like "24" will fail.
  • Missing leading zeros: The format %m expects "03" not "3". For single-digit months or days without leading zeros, use %-m or %-d (on some systems) or pad the input manually.
  • Case sensitivity: Month names must match exactly. "Jan" works with %b, but "JAN" does not.
  • Time zone awareness: Basic strptime does not handle time zones. For time zone strings like "2024-03-10 14:30:00 UTC", you need additional handling.

๐Ÿงช Testing Your Parsing

After parsing, you can verify the result by checking the object's attributes:

  • parsed_date.year returns the year (e.g., 2024)
  • parsed_date.month returns the month (e.g., 3)
  • parsed_date.day returns the day (e.g., 10)
  • parsed_date.hour returns the hour (e.g., 14)
  • parsed_date.weekday() returns the day of week (Monday=0)

This allows you to confirm that your format string correctly interpreted the input.


๐Ÿ’ก Quick Reference Workflow

  1. Identify the exact format of your date string
  2. Choose the appropriate format codes for each component
  3. Call datetime.strptime(your_string, your_format)
  4. Store the result as a datetime object
  5. Use the object for comparisons, arithmetic, or reformatting

Remember: The format string must match the input string character by character, including spaces, commas, and punctuation. When in doubt, test with a single example before processing large datasets.

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.

The strptime method converts a string representation of a date/time into a Python datetime object using a specified format pattern.


๐Ÿ“˜ Example 1: Parsing a Simple Date String

This example converts a date string in "YYYY-MM-DD" format into a datetime object.

from datetime import datetime

date_string = "2024-12-25"
date_object = datetime.strptime(date_string, "%Y-%m-%d")
print(date_object)

๐Ÿ“ค Output: 2024-12-25 00:00:00


๐Ÿ“˜ Example 2: Parsing a Date with Month Name

This example converts a date string that uses a full month name into a datetime object.

from datetime import datetime

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

๐Ÿ“ค Output: 2023-03-15 00:00:00


๐Ÿ“˜ Example 3: Parsing Date and Time Together

This example converts a string containing both date and time components into a datetime object.

from datetime import datetime

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

๐Ÿ“ค Output: 2024-01-10 14:30:00


๐Ÿ“˜ Example 4: Parsing a 12-Hour Time with AM/PM

This example converts a string with a 12-hour clock and AM/PM indicator into a datetime object.

from datetime import datetime

date_string = "07/25/2024 09:45 PM"
date_object = datetime.strptime(date_string, "%m/%d/%Y %I:%M %p")
print(date_object)

๐Ÿ“ค Output: 2024-07-25 21:45:00


๐Ÿ“˜ Example 5: Parsing a Log Timestamp with Timezone Offset

This example converts a common log format timestamp with timezone offset into a datetime object.

from datetime import datetime

date_string = "2024-11-03T08:15:30+0000"
date_object = datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S%z")
print(date_object)

๐Ÿ“ค Output: 2024-11-03 08:15:30+00:00


๐Ÿ“Š Common strptime Format Codes

Format Code Meaning Example
%Y 4-digit year 2024
%m 2-digit month (01-12) 03
%d 2-digit day (01-31) 15
%H 24-hour hour (00-23) 14
%I 12-hour hour (01-12) 09
%M 2-digit minute (00-59) 30
%S 2-digit second (00-59) 45
%p AM or PM AM
%B Full month name March
%b Abbreviated month name Mar
%z Timezone offset (+HHMM) +0000