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

Answer from Levon on Stack Overflow
Top answer
1 of 15
807

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 ... '%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

python - Convert string "Jun 1 2005 1:33PM" into datetime - Stack Overflow
There are many options for converting from the strings to Pandas Timestamps using to_datetime, so check the docs if you need anything special. Likewise, Timestamps have many properties and methods that can be accessed in addition to .date ... I think timings have changed by now (Python 3.9, pandas ... More on stackoverflow.com
🌐 stackoverflow.com
Convert String Date and Time to Datetime
Hi! yes! can't test it right now, but in my opiniton a change from then = datetime.strptime(eg_time, "%d.%m.%y %H:%M") to then = datetime.strptime(eg_time, "%d %B %y %H:%M") As documented in the datetime-docs the defnition string syntax for %m is a numeric month with a leading zero, while %B allows you to work with string represenations of months. More on reddit.com
🌐 r/learnpython
3
0
September 15, 2022
How To Convert a String to a datetime or time Object in Python
Learn everything about the Python datetime module. Find a step-by-step guide for strings to datetime conversion with examples. More on accuweb.cloud
🌐 accuweb.cloud
1
December 1, 2023
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
July 25, 2023
🌐
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
🌐
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
🌐
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.
🌐
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, ...
🌐
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 # Date String in dd/mm/yyyy HH:MM:SS format dt_string = "12/06/2021 09:15:32" # Convert string to datetime object dt_object = datetime.strptime(dt_string, "%d/%m/%Y %H:%M:%S") print(dt_object) # Output 2021-06-12 09:15:32Code language: Python (python) Run
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.to_datetime.html
pandas.to_datetime — pandas 3.0.1 documentation - PyData |
Convert dtypes. ... scalars can be int, float, str, datetime object (from stdlib datetime module or numpy). They are converted to Timestamp when possible, otherwise they are converted to datetime.datetime.
🌐
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'
🌐
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)
🌐
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:
🌐
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.
🌐
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.
🌐
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.
🌐
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)
🌐
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 - In Python, the `datetime` and `time` modules offer a convenient `strptime()` class method that allows you to convert strings into corresponding objects effortlessly.