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
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)
🌐
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.
🌐
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.
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.
🌐
CoenRaets
jsonviewer.ai › python-string-to-datetime
Best Ways to Convert Python String to DateTime[with Code] - JSON Viewer
July 29, 2023 - Each format has its representation for year, month, and day. Understanding these formats is crucial before converting strings to DateTime objects. Python’s datetime.strptime() method allows you to convert a date string into a DateTime object based on a specific format.
🌐
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:
🌐
TutorialsPoint
tutorialspoint.com › article › how-do-i-get-an-iso-8601-date-in-string-format-in-python
How do I get an ISO 8601 date in string format in Python?
June 16, 2025 - This returns the datetime object representing the Coordinated Universal Time (UTC). To get the desired string, we need to invoke the isoformat() method on the resultant object.
Find elsewhere
🌐
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.
🌐
Python documentation
docs.python.org › 3 › whatsnew › 3.14.html
What’s new in Python 3.14
1 month ago - Add the strptime() method to the datetime.date and datetime.time classes.
🌐
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.
🌐
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.
🌐
Code Beautify
codebeautify.org › blog › how-to-convert-string-to-date-using-to-python
How to Convert String to Date Using to Python
February 22, 2024 - We import the datetime module. Define the string representing the date (date_string) and the format (date_format) it is in. Use strptime() function to convert the string to a 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 - 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
🌐
freeCodeCamp
freecodecamp.org › news › strftime-in-python
strftime() Python – Datetime Format Tutorial
July 15, 2022 - from datetime import datetime date = datetime.now() string_time = date.strftime("%X") print(string_time) # 00:54:20 · The %X character formats a date string by showing the time representation in hours:minutes:seconds. In this tutorial, we've seen how to format date strings using different characters passed as an argument to the strftime date method. ... There are many other characters for full month names, abbreviated year names, and times. Check out the Python strftime cheatsheet to learn about more characters you can use to represent dates.
🌐
w3resource
w3resource.com › python-exercises › date-time-exercise › python-date-time-exercise-3.php
Python: Convert a string to datetime - w3resource
July 22, 2025 - The format of the input date string is specified as '%b %d %Y %I:%M%p', which indicates the month abbreviated (%b), day of the month (%d), year (%Y), hour in 12-hour format (%I), minute (%M), and AM/PM indicator (%p). ... Finally, it prints ...
🌐
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.
🌐
Home Assistant
community.home-assistant.io › t › converting-a-string-to-datetime-in-python › 141614
Converting a string to datetime in python - Home Assistant Community
July 21, 2019 - as imports are not allowed - ho do i do this? need to substract a datetime from another represented by string