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
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.
Discussions

Parse date string: Best way to parse string containing UTC
https://docs.python.org/3/library/datetime.html#datetime.datetime.fromisoformat or https://dateutil.readthedocs.io/en/stable/parser.html#dateutil.parser.parse More on reddit.com
🌐 r/learnpython
6
1
January 19, 2022
Converting an String to Datetime?
datetime.strptime is what you need. More on reddit.com
🌐 r/learnpython
1
1
February 7, 2021
Faster ways to convert from string to datetime?
Do you have a lot of duplicate dates in your dataframe? If so, you can create a date cache and then use map for faster parsing. Ref date_cache = {k: pd.to_datetime(k) for k in df['date'].unique()} df['date'] = df['date'].map(date_cache) More on reddit.com
🌐 r/learnpython
8
5
June 2, 2017
Unable to convert strings to datetime objects

I'll answer your question if you tell me which generator you used to come up with that user name.

More on reddit.com
🌐 r/bigquery
10
9
November 7, 2020
🌐
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 - For details about the format directives used in datetime.strptime(), refer to the strftime() and strptime() Format Codes in the Python documentation. The following example converts a date and time string into a datetime.datetime() object, and prints the class name and value of the resulting object: from datetime import datetime datetime_str = '09/19/22 13:55:26' datetime_object = datetime.strptime(datetime_str, '%m/%d/%y %H:%M:%S') print(type(datetime_object)) print(datetime_object) # printed in default format
🌐
DataCamp
datacamp.com β€Ί tutorial β€Ί converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - It automatically detects date formats, ... a format string. 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 ...
🌐
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. ... You can also convert a DateTime to a string for various purposes like reporting, custom formatting, ...
🌐
Readthedocs
dateparser.readthedocs.io
dateparser – python parser for human readable dates β€” DateParser 1.3.0 documentation
In case, when timezone is present both in string and also specified using Settings, string is parsed into tzaware representation and then converted to timezone specified in Settings. >>> parse('10:40 pm PKT', settings={'TIMEZONE': 'UTC'}) datetime.datetime(2017, 3, 12, 17, 40, tzinfo=<StaticTzInfo 'UTC'>) >>> parse('20 mins ago EST', settings={'TIMEZONE': 'UTC'}) datetime.datetime(2017, 3, 12, 21, 16, 0, 885091, tzinfo=<StaticTzInfo 'UTC'>) ... >>> from dateparser import parse >>> parse('December 2015') # default behavior datetime.datetime(2015, 12, 16, 0, 0) >>> parse('December 2015', settings={'PREFER_DAY_OF_MONTH': 'last'}) datetime.datetime(2015, 12, 31, 0, 0) >>> parse('December 2015', settings={'PREFER_DAY_OF_MONTH': 'first'}) datetime.datetime(2015, 12, 1, 0, 0)
Find elsewhere
🌐
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. Use dateparser.parse() for automatic datetime string parsing from various formats without manual format specification
🌐
IONOS
ionos.com β€Ί digital guide β€Ί websites β€Ί web development β€Ί convert strings to datetime in python
How to convert a string to datetime in Python - IONOS
December 17, 2024 - ... With the strptime() method, a string can be converted into a datetime.datetime object, taking into account a specific date/time format. from datetime import datetime date_string = '2023-10-30 12:00:00' date_object = datetime.strptime(da...
🌐
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)
🌐
Pandas
pandas.pydata.org β€Ί docs β€Ί reference β€Ί api β€Ί pandas.to_datetime.html
pandas.to_datetime β€” pandas 3.0.1 documentation - PyData |
The numeric values would be parsed as number of units (defined by unit) since this reference date. If 'unix' (or POSIX) time; origin is set to 1970-01-01. If 'julian', unit must be 'D', and origin is set to beginning of Julian Calendar. Julian day number 0 is assigned to the day starting at noon on January 1, 4713 BC. If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or date string), origin is set to Timestamp identified by origin.
🌐
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?

🌐
Tutorial Teacher
tutorialsteacher.com β€Ί articles β€Ί convert-string-to-datetime-in-python
Convert String to Datetime in Python
For example, '2020-10-16 12:35:20' is a string representing date and time. We can manually separate date and time attributes and construct the datetime object as follows: ... >>> strdate="2020-10-16 12:35:20" >>> dt_tuple=tuple([int(x) for x in strdate[:10].split('-')])+tuple([int(x) for x in strdate[11:].split(':')]) >>> dt_tuple (2020, 10, 16, 12, 35, 20) In the above datetime string, the first 10 characters have a year, month and day values separated by '-' and the remaining 8 characters have an hour, min and sec values separated by ':'. You can now unpack the above tuple for the datetime constructor by importing the datetime module, as shown below.
🌐
Programiz
programiz.com β€Ί python-programming β€Ί datetime β€Ί strptime
Python strptime() - string to datetime object
Based on the string and format code used, the method returns its equivalent datetime object. ... from datetime import datetime dt_string = "12/11/2018 09:15:32" # Considering date is in dd/mm/yyyy format dt_object1 = datetime.strptime(dt_string, "%d/%m/%Y %H:%M:%S") print("dt_object1 =", dt_object1) # Considering date is in mm/dd/yyyy format dt_object2 = datetime.strptime(dt_string, "%m/%d/%Y %H:%M:%S") print("dt_object2 =", dt_object2)
🌐
Programiz
programiz.com β€Ί python-programming β€Ί datetime β€Ί strftime
Python strftime() - datetime to string
Become a certified Python programmer. Try Programiz PRO! ... The strftime() method returns a string representing date and time using date, time or datetime object. The program below converts a datetime object containing current date and time to different string formats. from datetime import ...
🌐
Programiz
programiz.com β€Ί python-programming β€Ί examples β€Ί string-to-datetime
Python Program to Convert String to Datetime
If you want to learn more about ... print(type(date_time)) Output Β· 2011-03-11 11:31:00 <class 'datetime.datetime'> Using dateutil module, parse() can be used to convert a string into date time format....
🌐
freeCodeCamp
freecodecamp.org β€Ί news β€Ί how-to-convert-a-string-to-a-datetime-object-in-python
How to Convert a String to a DateTime Object in Python
December 17, 2024 - We need to import the datetime library in Python to be able to achieve this. We can do that by typing the following: from datetime import datetime date_string = "2018/08/09" format = %Y/%m/%d #specifify the format of the date_string. date = datetime.strptime(date_string, format) print(date) Let's go over the above code again to make sure we understand what's going on. The format variable declares the format of the date string to be passed to the parser (the function that will help us convert the date).
🌐
Educative
educative.io β€Ί answers β€Ί how-to-convert-a-string-to-a-date-in-python
How to convert a string to a date in Python
Python allows you to convert the string type date into a datetime object using strptime() method. The syntax for the method used to convert string to datetime object is strptime(x,y) where: ... y is the format directives in which the string ...
🌐
Stack Abuse
stackabuse.com β€Ί converting-strings-to-datetime-in-python
Converting Strings to datetime in Python
June 21, 2023 - While this is convenient, recall from earlier that having to predict the format makes the code much slower, so if your code requires high performance then this might not be the right approach for your application. Maya also makes it very easy to parse a string and change time zones. To easily convert a string with Python's Maya: import maya dt = maya.parse('2018-04-29T17:45:25Z').datetime() print(dt.date()) print(dt.time()) print(dt.tzinfo)
🌐
TestDriven.io
testdriven.io β€Ί tips β€Ί 44fef95e-e7ca-4da0-b14b-b5e3c8780036
Tips and Tricks - Parse datetime strings in Python with dateutil | TestDriven.io
You can use dateutil.parser to parse all sorts of different date and datetime string formats. ... from dateutil.parser import parse print(parse("2012-01-19 17:21:00")) # => 2012-01-19 17:21:00 print(parse("1st Jan, 2019")) # => 2019-01-01 00:00:00 print(parse("23.11.2020")) # => 2020-11-23 00:00:00
🌐
LabEx
labex.io β€Ί tutorials β€Ί python-how-to-parse-date-strings-in-python-398047
How to parse date strings in Python | LabEx
Regular expressions (regex) can be a powerful tool for parsing date strings, especially when dealing with complex or non-standard formats. The re module in Python provides a comprehensive set of functions for working with regular expressions. import re import datetime date_string = "April 25, 2023" pattern = r"(\w+) (\d+), (\d+)" match = re.match(pattern, date_string) if match: month = match.group(1) day = int(match.group(2)) year = int(match.group(3)) month_map = { "January": 1, "February": 2, "March": 3, "April": 4, "May": 5, "June": 6, "July": 7, "August": 8, "September": 9, "October": 10, "November": 11, "December": 12 } date_object = datetime.datetime(year, month_map[month], day) print(date_object) ## Output: 2023-04-25 00:00:00