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
๐ŸŒ
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 = ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ datetime โ€บ strftime
Python strftime() - datetime to string
In this article, you will learn to convert datetime object to its equivalent string in Python with the help of examples. For that, we can use strftime() method. Any object of date, time and datetime can call strftime() to get string from these objects.
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
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
Python date string to date object - Stack Overflow
How do I convert a string to a date object in python? The string would be: "24052010" (corresponding to the format: "%d%m%Y") I don't want a datetime.datetime object, but rather a datetime.date. 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
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_datetime.asp
Python Datetime
The datetime object has a method for formatting date objects into readable strings.
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 โ€บ datetime โ€บ strptime
Python strptime() - string to 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)
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - In Python, we can use the datetime.strptime() method to convert a string to a datetime object. The strptime() method takes two arguments: the string to be converted and a format string specifying the input string's format. The format string uses a combination of formatting codes to represent ...
Find elsewhere
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ datetime
Python datetime Module: Working with Dates, Times, and Formatting
To work with dates and times in Python, you import the datetime object from the datetime module. The two most common operations are converting strings to datetime objects (strptime) and formatting datetime objects into strings (strftime).
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.to_datetime.html
pandas.to_datetime โ€” pandas 3.0.2 documentation - PyData |
If Timestamp convertible (Timestamp, dt.datetime, np.datetimt64 or date string), origin is set to Timestamp identified by origin.
๐ŸŒ
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. Two arguments are required: the string to be parsed and the date/time format of the string.
๐ŸŒ
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 - To convert a date string in the mm dd yyyy format to a datetime object in Python, you can use the datetime.strptime method from the datetime module:
๐ŸŒ
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.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ articles โ€บ convert-string-to-datetime-in-python
Convert String to Datetime in Python
For example, the default string representation of the date is 'YYYY-MM-DD', for time 'HH:MM:SS' and 'YYYY-MM-DD HH:MM:SS' is for the datetime.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ converting-string-yyyy-mm-dd-into-datetime-in-python
Converting string into DateTime in Python - GeeksforGeeks
July 23, 2025 - Explanation: Here, we passed the date string '2023-07-25' to parse(). It automatically recognized the format and returned a full datetime object. datetime.strptime() method, part of Python's datetime module, efficiently converts a date string into a DateTime object when the exact format is known, requiring a format specification like '%Y/%m/%d'.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-strftime-function
Python strftime() function - GeeksforGeeks
July 12, 2025 - Return Type: It returns the string representation of the date or time object. In this example, we demonstrate how to extract different parts of the date and time using various format codes like weekday names, month, year, hour, and day of the year.
๐ŸŒ
Dataquest
dataquest.io โ€บ blog โ€บ python-datetime
Python Datetime: A Simple Guide with 39 Code Examples (2023)
January 5, 2026 - In Python, we can also convert a datetime object into a string using the strftime() function. It takes two arguments: a datetime object and the format of the output string. # Create a datetime object datetime_obj = datetime(2022, 12, 31) # American ...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-datetime-to-string
Learn Python: Convert datetime to string (With Examples)
February 7, 2024 - Then, we get the current date and time using datetime.now(). After that, we specify the format in which we want our date and time to be converted into a string. Finally, we use the strftime() function to convert the datetime object into a string. As ...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python โ€บ python datetime โ€บ python datetime format using strftime()
Python DateTime Format using Strftime() โ€“ PYnative
May 6, 2022 - This tutorial will teach how to ... of a datetime module and time module. The strftime() method returns a string representing of a datetime object according to the format codes....