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
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.to_datetime.html
pandas.to_datetime โ€” pandas documentation
If False, allow the format to match anywhere in the target string. Cannot be used alongside format='ISO8601' or format='mixed'. ... The unit of the arg (D,s,ms,us,ns) denote the unit, which is an integer or float number. This will be based off the origin. Example, with unit='ms' and origin='unix', this would calculate the number of milliseconds to the unix epoch start. ... Define the reference date.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ converting-a-string-to-a-datetime
Converting a string to a datetime - Python Morsels
September 30, 2024 - The strftime method does the opposite of strptime: it converts datetime objects to strings, allowing us to specify the format we'd like to use during that conversion. >>> moment.strftime("%a, %d %b %Y %H:%M:%S") 'Wed, 31 Oct 2046 15:46:10' However, ...
๐ŸŒ
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: Dec 4 2018 10:07:00AM Explanation: In this, we are converting the datetime format to the string ยท Python strftime() function is present in datetime module to create a string representation based on the specified ...
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.
๐ŸŒ
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.
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - In all of these cases we showed, we used datetime objects, but the real-world data often remains as a string in practice. And converting to a datetime object unlocks all of the above functionalities that are powerful in data science analysis and applications. In Python, we can use the datetime.st...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 1299895 โ€บ how-to-convert-datetime-to-date-string-string-only
How to convert datetime to date string string only - Microsoft Q&A
You can customize the format string according to your desired date format. The %Y represents the four-digit year, %m represents the month, and %d represents the day. You can find more format codes in the Python documentation for strftime(). If you have a specific datetime object instead of the current datetime, replace current_datetime in the example with your datetime object. from datetime import datetime # Current datetime current_datetime = datetime.now() # Convert datetime to date string date_string = current_datetime.strftime('%Y-%m-%d') print(date_string)
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ get-todays-date-and-convert-to-string
Here is how to get today's date and convert to string in Python
# Get today's date today = datetime.date.today() # Format the date as a string in the format YYYY-MM-DD formatted_date = today.strftime("%Y-%m-%d")
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ library โ€บ datetime.html
datetime โ€” Basic date and time types
Added in version 3.8. ... Return a date corresponding to date_string, parsed according to format.
๐ŸŒ
PythonHow
pythonhow.com โ€บ how โ€บ convert-a-string-into-datetime-format
Here is how to convert a string into datetime format in Python
You can then use this object in your code as needed.It's worth noting that the exact format of the date and time string can vary depending on the conventions used in your region. The strptime() method uses the same format codes as the strftime() method, which you can use to convert datetime ...
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ datetime-from-string-python
How to Convert String to DateTime in Python? (with code)
March 10, 2023 - Learn how to convert string to date or date to string in python with code. Also, we will understand datetime and time modules in python.
๐ŸŒ
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'
๐ŸŒ
Studytonight
studytonight.com โ€บ python-howtos โ€บ how-to-convert-date-object-to-string-in-python
How to convert date object to string in Python - Studytonight
In python, there is a function called strftime() which converts a datetime, time, and date object into a string object according to the given format.