Python datetime.strptime() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
classmethod

Definition

datetime.strptime(date_string, format) reads date_string using the layout described by format and returns a new datetime. It uses the same % codes as strftime(), in reverse.

Fields missing from the format get defaults: year 1900, month 1, day 1, and time 00:00:00. The result is naive unless the format includes %z and the text has an offset.

datetime.strptime() is a classmethod of datetime.datetime: call it on the class itself, as datetime.strptime(...), and it returns a new object. Import the class first, for example from datetime import datetime.

Syntax

Pass the text and a format string that matches it exactly.

python
moment = datetime.strptime(date_string, format)
CallWhat it does
datetime.strptime("2024-03-15", "%Y-%m-%d")Returns datetime.datetime(2024, 3, 15, 0, 0).
datetime.strptime("15/03/24 18:05", "%d/%m/%y %H:%M")Returns datetime.datetime(2024, 3, 15, 18, 5).
datetime.strptime("2024-02-30", "%Y-%m-%d")Raises ValueError: February has no 30th day.

Spaces in the format match any run of whitespace, and letters such as month names match case-insensitively.

Method signatures
python
classmethod datetime.strptime(date_string, format)
# date_string: str to parse
# format: str of literal text and % codes
# returns: datetime (aware only when %z matched an offset)

Parameters

ParameterDescription
date_stringThe text to parse, such as "2024-03-15 10:30".
formatThe layout, such as "%Y-%m-%d %H:%M". Every character outside a code must appear in the text literally.

Return Value

Return ValueDescription
datetimeA new datetime built from the parsed fields.

Python Examples

Example 1 — Parse Three Common Date Layouts

Read an SQL-style timestamp, a day/month/year date, and a compact layout without separators.

python
from datetime import datetime

print(datetime.strptime("2024-03-15 10:30:45", "%Y-%m-%d %H:%M:%S"))
print(datetime.strptime("15/03/2024", "%d/%m/%Y"))
print(datetime.strptime("20240315T1030", "%Y%m%dT%H%M"))

How It Works

  1. Codes mirror the text. %Y-%m-%d %H:%M:%S lines up with each part of 2024-03-15 10:30:45.
  2. Missing time. A format without hour codes leaves the time at midnight.
  3. No separators needed. %Y takes four digits and %m, %d, %H, %M take two each, so the compact string splits correctly; the literal T must match too.

Example 2 — Parse Month Names, Weekdays, and AM/PM

Parse English text with a weekday, a full month name, and a 12-hour clock. This assumes the default C/English locale.

python
from datetime import datetime

text = "Friday, 15 March 2024 at 07:05 PM"
moment = datetime.strptime(text, "%A, %d %B %Y at %I:%M %p")
print(moment)
print(datetime.strptime("Mar 3 2024", "%b %d %Y").date())
print(datetime.strptime("12:15 AM", "%I:%M %p").time())

How It Works

  1. Literal words. The comma and the word at in the format must appear in the text exactly.
  2. 12-hour clock. %I with %p turns 07:05 PM into 19:05.
  3. Single-digit day. %d also accepts 3 without a leading zero.
  4. Midnight hour. 12:15 AM is 00:15, shown by keeping only the time() part.

Example 3 — Parse a UTC Offset and Reject Bad Input

Use %z to get an aware result, then see two inputs that raise ValueError.

python
from datetime import datetime

aware = datetime.strptime("2024-03-15 10:30 +0530", "%Y-%m-%d %H:%M %z")
print(aware)
print(aware.tzname())

for text in ("2024-02-30", "2024-03-15 extra"):
    try:
        datetime.strptime(text, "%Y-%m-%d")
    except ValueError:
        print("rejected:", text)

How It Works

  1. Offset becomes a zone. %z reads +0530 and attaches a fixed-offset timezone, so the result is aware.
  2. Generated name. The offset has no name, so tzname() reports UTC+05:30.
  3. Impossible date. The text matches the pattern, but 30 February does not exist, so ValueError is raised.
  4. Leftover text. Extra characters after the last code also raise ValueError.

Frequently Asked Questions

Use datetime.strptime(text, format), for example datetime.strptime("15/03/2024", "%d/%m/%Y").
The format matched the start of the text, but characters were left over. Add codes or literals for the rest.
Add %z. It accepts +0530, +05:30, and Z, and makes the result aware.
You used import datetime. Write datetime.datetime.strptime(...), or import the class with from datetime import datetime.
Use fromisoformat() for ISO 8601 text; it is simpler and faster. Use strptime() for any other layout.

Next: datetime.time()

Return the clock part of a datetime as a naive time object.

time() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful