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)
๐ŸŒ
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.
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
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
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
๐ŸŒ
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.
๐ŸŒ
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....
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.
Find elsewhere
๐ŸŒ
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
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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)
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
SW Hosting
swhosting.com โ€บ en โ€บ blog โ€บ dates-and-times-in-python-converting-strings-to-datetime-and-time-objects
Dates and Times in Python: Converting Strings to datetime and time Objects - SW Hosting's Blog
June 13, 2024 - In this article, you will learn how to convert a string to a datetime or time object with practical and detailed examples. The Python datetime module provides classes for working with dates and times.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ whatsnew โ€บ 3.14.html
Whatโ€™s new in Python 3.14
February 23, 2026 - Add the strptime() method to the datetime.date and datetime.time classes.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ convert string to datetime in python
How To Convert a String to a datetime in Python? - Scaler Topics
April 28, 2022 - This article covers converting the date, time & datetime object into string using strftime() function and converting the string into a datetime object using strptime() function.
๐ŸŒ
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.