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
Return a date corresponding to a date_string given in any valid ISO 8601 format, with the following exceptions: Reduced precision dates are not currently supported (YYYY-MM, YYYY). Extended date representations are not currently supported (±YYYYYY-MM-DD). Ordinal dates are not currently supported (YYYY-OOO). ... >>> import datetime as dt >>> dt.date.fromisoformat('2019-12-04') datetime.date(2019, 12, 4) >>> dt.date.fromisoformat('20191204') datetime.date(2019, 12, 4) >>> dt.date.fromisoformat('2021-W01-1') datetime.date(2021, 1, 4)
🌐
Programiz
programiz.com › python-programming › datetime › strftime
Python strftime() - datetime to string
We also recommend you to check Python strptime(). The strptime() method creates a datetime object from a string.
🌐
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 - In this article, you’ll use strptime() to convert strings into datetime and struct_time() objects. Deploy your Python applications from GitHub using DigitalOcean App Platform.
🌐
Stack Abuse
stackabuse.com › converting-strings-to-datetime-in-python
Converting Strings to datetime in Python
June 21, 2023 - In this tutorial, we'll be converting Strings to datetime in Python, dealing with Timezones. We'll also use dateutil, Maya and Arrow to convert Strings to datetime with automatic format recognition.
🌐
Educative
educative.io › answers › how-to-convert-a-string-to-a-date-in-python
How to convert a string to a date in Python
The syntax for the method used to convert string to datetime object is strptime(x,y) where:
🌐
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 - The strptime() (string parse time) method from the datetime library is used to convert a string into a Python datetime object.
🌐
DataCamp
datacamp.com › tutorial › converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - Learn how to convert strings to datetime objects in Python using strptime(), dateutil, and pandas. Includes code examples, timezone handling, and troubleshooting tips.
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 - To create a string in the desired format, use the DateTime object's strftime() method. The format string is the argument for the strftime() function. Example: The program imports the datetime module, which gives Python programmers access to ...
Top answer
1 of 15
806

You can use strftime to help you format your date.

E.g.,

import datetime
t = datetime.datetime(2012, 2, 23, 0, 0)
t.strftime('%m/%d/%Y')

will yield:

'02/23/2012'

More information about formatting see here

2 of 15
325

date and datetime objects (and time as well) support a mini-language to specify output, and there are two ways to access it:

  • direct method call: dt.strftime('format here')
  • format method (python 2.6+): '{:format here}'.format(dt)
  • f-strings (python 3.6+): f'{dt:format here}'

So your example could look like:

  • dt.strftime('The date is %b %d, %Y')
  • 'The date is {:%b %d, %Y}'.format(dt)
  • f'The date is {dt:%b %d, %Y}'

In all three cases the output is:

The date is Feb 23, 2012

For completeness' sake: you can also directly access the attributes of the object, but then you only get the numbers:

'The date is %s/%s/%s' % (dt.month, dt.day, dt.year)
# The date is 02/23/2012

The time taken to learn the mini-language is worth it.


For reference, here are the codes used in the mini-language:

  • %a Weekday as locale’s abbreviated name.
  • %A Weekday as locale’s full name.
  • %w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.
  • %d Day of the month as a zero-padded decimal number.
  • %b Month as locale’s abbreviated name.
  • %B Month as locale’s full name.
  • %m Month as a zero-padded decimal number. 01, ..., 12
  • %y Year without century as a zero-padded decimal number. 00, ..., 99
  • %Y Year with century as a decimal number. 1970, 1988, 2001, 2013
  • %H Hour (24-hour clock) as a zero-padded decimal number. 00, ..., 23
  • %I Hour (12-hour clock) as a zero-padded decimal number. 01, ..., 12
  • %p Locale’s equivalent of either AM or PM.
  • %M Minute as a zero-padded decimal number. 00, ..., 59
  • %S Second as a zero-padded decimal number. 00, ..., 59
  • %f Microsecond as a decimal number, zero-padded on the left. 000000, ..., 999999
  • %z UTC offset in the form +HHMM or -HHMM (empty if naive), +0000, -0400, +1030
  • %Z Time zone name (empty if naive), UTC, EST, CST
  • %j Day of the year as a zero-padded decimal number. 001, ..., 366
  • %U Week number of the year (Sunday is the first) as a zero padded decimal number.
  • %W Week number of the year (Monday is first) as a decimal number.
  • %c Locale’s appropriate date and time representation.
  • %x Locale’s appropriate date representation.
  • %X Locale’s appropriate time representation.
  • %% A literal '%' character.
🌐
Programiz
programiz.com › python-programming › examples › string-to-datetime
Python Program to Convert String to Datetime
To understand this example, you should have the knowledge of the following Python programming topics: ... from datetime import datetime my_date_string = "Mar 11 2011 11:31AM" datetime_object = datetime.strptime(my_date_string, '%b %d %Y %I:%M%p') print(type(datetime_object)) print(datetime_object)
🌐
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 - import time # time hours-minutes-seconds format time_string = "09-15-09" format_codes = "%H-%M-%S" time_obj = time.strptime(time_string, format_codes) print("Time Object", time_obj) print(type(time_obj))Code language: Python (python) Run ... Time Object time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=9, tm_min=15, tm_sec=9, tm_wday=0, tm_yday=1, tm_isdst=-1) <class 'time.struct_time'> This method basically converts the string into a datetime object according to a format.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.1 documentation - PyData |
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.
🌐
Programiz
programiz.com › python-programming › datetime › strptime
Python strptime() - string to datetime object
The string needs to be in a certain format. from datetime import datetime date_string = "21 June, 2018" print("date_string =", date_string) print("type of date_string =", type(date_string)) date_object = datetime.strptime(date_string, "%d %B, %Y") print("date_object =", date_object) print("type of date_object =", type(date_object))
🌐
Tutorial Teacher
tutorialsteacher.com › articles › convert-string-to-datetime-in-python
Convert String to Datetime in Python
Let's use the strptime() method to convert a given string to a datetime object, as shown below:
🌐
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 - The solution to this problem is to parse (or convert) the string object into a datetime object so Python can recognized it as a date.
🌐
Mimo
mimo.org › glossary › python › datetime
Mimo: The coding platform you need to learn Web Development, Python, and more.
In this example, the string 2024-09-19 15:45:30 is converted into a datetime object using the format "%Y-%m-%d %H:%M:%S". You can adjust the format string to match different date and time formats.
🌐
InfluxData
influxdata.com › home › how to convert string to datetime in python
How to Convert String to Datetime in Python | InfluxData
October 30, 2023 - Converting between text and types related to dates and times is a commonplace task in programming languages, and Python is certainly no exception. That’s why in this post we’ll treat you to a string-to-datetime Python conversion tutorial.
🌐
freeCodeCamp
freecodecamp.org › news › python-string-to-datetime-how-to-convert-an-str-to-a-date-time-with-strptime
Python String to Datetime – How to Convert an Str to a Date Time with Strptime
February 2, 2023 - Hopefully, this article helped you understand how to convert a string to a datetime object in Python using the strptime() method.
🌐
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 - You can convert a string to a datetime or time object in Python using the datetime module, which provides various functions and classes for working with date and time values.