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.
🌐
PYnative
pynative.com › home › python › python datetime › python datetime format using strftime()
Python DateTime Format using Strftime() – PYnative
May 6, 2022 - from datetime import datetime # Get current Date x_date = datetime.now() print('Current Date:', x_date) # Represent Dates in full textual format print("dd-MonthName-yyyy:", x_date.strftime("%d-%B-%Y")) print("DayName-dd-MonthName-yyyy:", x_date.strftime("%A,%d %B, %Y")) # Represent dates in short textual format print("dd-MonthName-yyyy:", x_date.strftime("%d-%b-%Y")) print("DDD-dd-MMM-yyyy:", x_date.strftime("%a,%d %b, %Y"))Code language: Python (python) Run ... Current Date: 2021-07-07 12:19:47.864519 dd-MonthName-yyyy: 07-July-2021 DayName-dd-MonthName-yyyy: Wednesday,07 July, 2021 dd-MonthName-yyyy: 07-Jul-2021 DDD-dd-MMM-yyyy: Wed,07 Jul, 2021 · The strftime() method can be called using the date, time, or datetime objects. Let’s how to format only date object of a datetime module to string.
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.
🌐
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 - The Python datetime and time modules both include a strptime() class method to convert strings to objects.
🌐
W3Schools
w3schools.com › python › python_datetime.asp
Python Datetime
The datetime() class requires three parameters to create a date: year, month, day. ... The datetime() class also takes parameters for time and timezone (hour, minute, second, microsecond, tzone), but they are optional, and has a default value of 0, (None for timezone). The datetime object has a method for formatting date objects into readable strings.
🌐
Educative
educative.io › answers › how-to-convert-a-string-to-a-date-in-python
How to convert a string to a date in Python
Line 7–9: Uses strptime() method for converting the date (stored in dateString variable) in a datetime object. The converted date is then stored in the dateTimeObj variable. The following figure explains the use of appropriate directives in ...
🌐
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 - Input: 2018-12-04 10:07:00 Output: ... format to the string · Python strftime() function is present in datetime module to create a string representation based on the specified format string....
Find elsewhere
🌐
Mimo
mimo.org › glossary › python › datetime
Mimo: The coding platform you need to learn Web Development, Python, and more.
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).
🌐
freeCodeCamp
freecodecamp.org › news › strftime-in-python
strftime() Python – Datetime Format Tutorial
July 15, 2022 - datetime.now returns the current date. Using the strftime method and the "%Y" character, the date is converted to a string showing the year.
🌐
iO Flood
ioflood.com › blog › python-datetime-to-string
Learn Python: Convert datetime to string (With Examples)
February 7, 2024 - So, buckle up and get ready to dive into the world of Python datetime conversions! You can use the strftime() function to convert datetime to a string in Python.
🌐
Python Morsels
pythonmorsels.com › converting-a-string-to-a-datetime
Converting a string to a datetime - Python Morsels
September 30, 2024 - That name stands for "string parse time" (str-p-time). We'll also see strftime in a moment, which stands for "string format time" (str-f-time). The datetime class has a strptime method for parsing strings into datetime objects.
🌐
Stack Abuse
stackabuse.com › converting-strings-to-datetime-in-python
Converting Strings to datetime in Python
June 21, 2023 - You can either opt for the default Python datetime library or any of the third-party libraries mentioned in this article, among many others. The main problem with the default datetime package is that we need to specify the parsing code manually for almost all date-time string formats.
🌐
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.
🌐
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.
🌐
strftime
strftime.org
Python strftime reference cheatsheet
A quick reference for Python's strftime formatting directives.