datetime.strptime parses an input string in the user-specified format into a timezone-naive datetime object:

>>> from datetime import datetime
>>> datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')
datetime.datetime(2005, 6, 1, 13, 33)

To obtain a date object using an existing datetime object, convert it using .date():

>>> datetime.strptime('Jun 1 2005', '%b %d %Y').date()
date(2005, 6, 1)

Links:

  • strptime docs: Python 2, Python 3

  • strptime/strftime format string docs: Python 2, Python 3

  • strftime.org format string cheatsheet

Notes:

  • strptime = "string parse time"
  • strftime = "string format time"
Answer from Patrick Harrington on Stack Overflow
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ datetime.html
datetime โ€” Basic date and time types
Such format values may raise an error as of Python 3.15. The workaround is to always include a year in your format. If parsing date_string values that do not have a year, explicitly add a year that is a leap year before parsing: >>> import datetime as dt >>> date_string = "02/29" >>> when = dt.date.strptime(f"{date_string};1984", "%m/%d;%Y") # Avoids leap year bug.
๐ŸŒ
dateutil
dateutil.readthedocs.io โ€บ en โ€บ stable โ€บ parser.html
parser โ€” dateutil 3.9.0 documentation - Read the Docs
This module offers a generic date/time string parser which is able to parse most known formats to represent a date and/or time. This module attempts to be forgiving with regards to unlikely input formats, returning a datetime object even for dates which are ambiguous.
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python datetime โ€บ python string to datetime using strptime()
Python String to DateTime using Strptime() [5 Ways] โ€“ PYnative
December 5, 2021 - from datetime import datetime # String with full day and month name date_str = "Wednesday,10 February,2021 12:19:47" # %A is to parse weekday and %B to parse month name dt_obj = datetime.strptime(date_str, "%A,%d %B,%Y %H:%M:%S") print("Date Object:", dt_obj) # Output 2021-02-10 12:19:47 Code language: Python (python) Run
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-to-datetime-strptime
How To Convert a String to a datetime Object in Python | DigitalOcean
December 13, 2024 - The ValueError messages that you get when you use the strptime() method clearly explain the root causes of the parsing errors. The following example demonstrates some common errors, such as extra data and a format mismatch: from datetime import datetime import time datetime_str = '09/19/18 ...
๐ŸŒ
Scrapfly
scrapfly.io โ€บ blog โ€บ posts โ€บ parsing-datetime-strings-with-python-and-dateparser
How to Parse Datetime Strings with Python and Dateparser - Scrapfly Blog
June 9, 2023 - Master Python dateparser for converting string to datetime objects with automatic format detection, timezone handling, and advanced parsing configurations for web scraping data processing.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-convert-string-to-datetime-and-vice-versa
Convert string to DateTime and vice-versa in Python - GeeksforGeeks
July 11, 2025 - The Parser.parse() function recognizes the format automatically, which transforms the string "Jun 23 2022 07:31 PM" into a DateTime object.
๐ŸŒ
Readthedocs
dateparser.readthedocs.io
dateparser โ€“ python parser for human readable dates โ€” DateParser 1.3.0 documentation
>>> # parsing ambiguous date >>> parse('02-03-2016') # assumes english language, uses MDY date order datetime.datetime(2016, 2, 3, 0, 0) >>> parse('le 02-03-2016') # detects french, uses DMY date order datetime.datetime(2016, 3, 2, 0, 0) ... Ordering is not locale based, thatโ€™s why do not expect DMY order for UK/Australia English. You can specify date order in that case as follows using Settings: >>> parse('18-12-15 06:00', settings={'DATE_ORDER': 'DMY'}) datetime.datetime(2015, 12, 18, 6, 0)
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ parse date string: best way to parse string containing utc
r/learnpython on Reddit: Parse date string: Best way to parse string containing UTC
January 19, 2022 -

Hi.

Consider this text string: 2021-01-01 01:01:00 UTC.

What's the best way of parsing this into a date object in Python 3? I could remove the "UTC" from the text, and then use something like datetime.strptime(date_string, format), but isn't there an easier way, like doesn't Python already have support for parseing strings like these?

๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ converting-a-string-to-a-datetime
Converting a string to a datetime - Python Morsels
September 30, 2024 - Trey Hunner 4 min. read โ€ข Python 3.10โ€”3.14 โ€ข Sept. 30, 2024 ... Copied to clipboard. Tags ... You need the strptime class method. That's the easy part. The hard part is figuring out which format string you need to specify to parse your date string properly. Here's an example of the strptime class method in action: >>> from datetime import datetime >>> datetime.strptime("Jun 1 2005 1:33PM", "%b %d %Y %I:%M%p") datetime.datetime(2005, 6, 1, 13, 33)
๐ŸŒ
Statology
statology.org โ€บ home โ€บ how to parse dates from text in python
How to Parse Dates from Text in Python
August 5, 2024 - The most common way to parse dates in Python is using the strptime() function from the datetime module.
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ parse-datetime-strings-with-parsedatetime-in-python
Parse Datetime Strings with parsedatetime in Python
February 27, 2023 - The first, and most common way to use parsedatetime is to parse a string into a datetime object. First, you'll want to import the parsedatetime library, and instantiate a Calendar object, which does the actual input, parsing and manipulation of dates:
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ convert string date and time to datetime
r/learnpython on Reddit: Convert String Date and Time to Datetime
September 15, 2022 -

I am trying to convert a date and time formatted as a string to datetime. I've read a few articles and thought this would work but it throws the error below. Can anyone see what the issue is?

Thanks

eg_time = '15 September 2022 19:00'
then = datetime.strptime(eg_time, "%d.%m.%y %H:%M")

Error:

Traceback (most recent call last):
  File "C:/", line 75, in <module>
    then = datetime.strptime(eg_time, "%d.%m.%y %H:%M")
  File "C:", line 577, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "C:", line 359, in _strptime
    (data_string, format))
ValueError: time data '15 September 2022 19:00' does not match format '%d.%m.%y %H:%M'
๐ŸŒ
GitHub
github.com โ€บ RhetTbull โ€บ strpdatetime
GitHub - RhetTbull/strpdatetime: Parse strings into Python datetime objects; extends Python's datetime.strptime() with additional features. ยท GitHub
>>> from strpdatetime import strpdatetime >>> strpdatetime("IMG_1234_2022_11_20.jpg","^IMG_{4}_%Y_%m_%d") datetime.datetime(2022, 11, 20, 0, 0) >>> strpdatetime("IMG_1234_2022_11_20.jpg","IMG_*_%Y_%m_%d") datetime.datetime(2022, 11, 20, 0, 0) >>> strpdatetime("1234_05_06_2022_11_20","%Y_%m_%d$") datetime.datetime(2022, 11, 20, 0, 0) >>> strpdatetime("1234_05_06_2022_11_20","IMG_*_%Y_%m_%d|%Y_%m_%d$") datetime.datetime(2022, 11, 20, 0, 0) >>> strpdatetime includes a very simple command line interface. It can be used to test the regex-like syntax. Use python -m strpdatetime --help for more information.
Author ย  RhetTbull
๐ŸŒ
Accuweb
accuweb.cloud โ€บ home โ€บ how to convert a string to a datetime or time object in python
How To Convert a String to a datetime or time Object in Python - AccuWeb Cloud
December 1, 2023 - To convert a string to a datetime or time object in Python, use datetime.strptime() with the appropriate format string. This allows you to parse the string into a datetime object, which can then be used for time-based operations.
๐ŸŒ
TestDriven.io
testdriven.io โ€บ tips โ€บ 44fef95e-e7ca-4da0-b14b-b5e3c8780036
Tips and Tricks - Parse datetime strings in Python with dateutil | TestDriven.io
from dateutil.parser import parse %timeit parse(datetime_txt).date() 54.3 ยตs ยฑ 450 ns per loop (mean ยฑ std. dev. of 7 runs, 10000 loops each) import datetime %timeit datetime.datetime.strptime(datetime_txt, "%H:%M %d-%m-%Y").date() 7.44 ยตs ยฑ 240 ns per loop (mean ยฑ std.
๐ŸŒ
Luasoftware
code.luasoftware.com โ€บ tutorials โ€บ python โ€บ python-parse-string-to-date
Python Parse String to datetime - Lua Software Code
August 7, 2019 - date_str = 'Sat Jul 11 21:09:55 2019'd = dateutil_parser.parse(date_str) ... date_str = '2019-08-07T15:36:11.618911Z'# ordate_str = '2019-08-07T15:36:11.618Z'# ordate_str = '2019-08-07T15:36:11Z' If you know the miliseconds decimals and whether it contains timezone or not ยท import datetimedate_str = '2019-08-07T15:36:11Z'datetime.datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S%z")
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-datetime-strptime-function
Python DateTime - strptime() Function - GeeksforGeeks
January 12, 2026 - This example shows how to convert a date in string format into a datetime.date object using Pythonโ€™s strptime() function.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - from dateutil.parser import parse # Automatically infers the format date_obj = parse("March 1, 2023 9:30 AM") print(date_obj) This is especially useful when dealing with unpredictable or inconsistent date formats. For timezone-aware datetime handling, Python offers two popular options:
๐ŸŒ
Inventive HQ
inventivehq.com โ€บ home โ€บ blog โ€บ python datetime format: strftime, strptime & date conversion examples
Python Datetime Format: strftime, strptime & Date Conversion Examples
November 6, 2025 - For example, the date string "7/11/2019" would use the mask "%m/%d/%Y" to indicate month/day/year format separated by forward slashes. The strptime() function (String Parse Time) is your primary tool for converting date strings into datetime objects.