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
🌐
Programiz
programiz.com › python-programming › examples › string-to-datetime
Python Program to Convert String to Datetime
To understand this example, you ... '%b %d %Y %I:%M%p') print(type(datetime_object)) print(datetime_object) ... Using strptime(), date and time in string format can be converted to datetime type....
Discussions

Convert datetime object to a String of date only in Python - Stack Overflow
I see a lot on converting a date string to an datetime object in Python, but I want to go the other way. I've got datetime.datetime(2012, 2, 23, 0, 0) and I would like to convert it to string lik... More on stackoverflow.com
🌐 stackoverflow.com
reformat datetime object
without converting it to a string All I'm looking to do is convert a datetime formatted as %Y-%m-%d to datetime %m/%d%Y I don't understand, that is converting to a string. More on reddit.com
🌐 r/learnpython
3
0
June 24, 2023
Converting an String to Datetime?
datetime.strptime is what you need. More on reddit.com
🌐 r/learnpython
1
1
October 23, 2020
What's a quick way to convert a date time stamp into RFC 822 format?
Use datetime.datetime.strptime to convert your string to a datetime object, then use the strftime method on that object to get the result you want. Check this handy site for the formatting strings: http://strftime.org/ More on reddit.com
🌐 r/Python
6
2
January 1, 2017
🌐
Sentry
sentry.io › sentry answers › python › convert a string containing a date into datetime in python
Convert a string containing a date into datetime in Python | Sentry
March 15, 2023 - from datetime import datetime my_datetime_string = 'Feb 2 2020 2:05PM' datetime_format = '%b %d %Y %I:%M%p' my_datetime = datetime.strptime(my_datetime_string, datetime_format) print(my_datetime) # will produce "2020-02-02 14:05:00" ... Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski. SEE EPISODES ... David Y. — March 15, 2023 · Remove DataFrame rows with missing values in Python
🌐
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.
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.
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 - Create a format string that outlines the formatting requirements for the datetime object. 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.
🌐
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.
🌐
Medium
medium.com › @generativeai.saif › how-to-convert-a-string-to-a-date-in-python-complete-guide-for-2025-bd5ece07c57c
How to Convert a String to a Date in Python: Complete Guide for 2025 | by Saif Ali | Medium
April 6, 2025 - Now that we understand the basics, let’s explore how to convert various common date formats you’ll encounter in real-world applications. Each format requires a specific combination of directives to properly parse the date string. The ISO 8601 date format is widely used in international contexts and is considered a standard for data exchange. Here’s how to parse this common format: from datetime import datetime date_string = "2025-04-02" date_object = datetime.strptime(date_string, "%Y-%m-%d") print(date_object) # 2025-04-02 00:00:00
🌐
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.
🌐
iO Flood
ioflood.com › blog › convert-string-to-date
Convert String to Date in Python: Object Handling Tutorial
March 11, 2024 - In this example, we import the datetime module and use the strptime() function to convert a string to a date. The string ‘2022-01-01’ is parsed according to the format ‘%Y-%m-%d’, resulting in the output ‘2022-01-01 00:00:00’. But ...
🌐
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.
🌐
Analytics Vidhya
analyticsvidhya.com › home › convert string to datetime and vice-versa in python
Convert String to DateTime and Vice-Versa in Python - Analytics Vidhya
February 6, 2024 - Explore methods to seamlessly convert strings to DateTime objects and vice-versa. Best practices and code examples included.
🌐
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.
🌐
PythonHow
pythonhow.com › how › convert-a-string-into-datetime-format
Here is how to convert a string into datetime format in Python
To convert a string into a datetime object in Python, you can use the strptime() method from the datetime module. This method takes the string to be converted and the format in which the string represents a date and time, and it returns a datetime object that represents the same date and time.
🌐
How To Guides
hostingseekers.com › home › how to convert a string to a datetime object in python?
How To Convert a String to a DateTime Object in Python?
January 31, 2025 - Call datetime.strptime() with the string and format as arguments. ... Now date_object is a datetime object, and you can use it for further processing. ... The Python datetime module empowers developers to effortlessly convert between strings and datetime objects, providing precise control over date and time manipulation.
🌐
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.
🌐
Vultr
docs.vultr.com › python › examples › convert-string-to-datetime
Python Program to Convert String to Datetime | Vultr Docs
November 22, 2024 - This script attempts to parse a string with an incorrect date format and handles any ValueError by printing an error message. Converting strings into datetime objects in Python is an essential task for many applications involving date manipulations. By using Python's datetime.strptime() method, you can handle various date string formats efficiently.
🌐
Tutorial Teacher
tutorialsteacher.com › articles › convert-string-to-datetime-in-python
Convert String to Datetime in Python
If the date string is changed to 'DD-MM-YY', the format has to be set to %d-%m-%Y. ... >>> strdate="16-10-2020 12:35:20" >>> datetimeobj=datetime.datetime.strptime(strdate,"%d-%m-%Y %H:%M:%S") >>> datetimeobj datetime.datetime(2020, 10, 16, ...