Your example code is wrong. This works:

import datetime

datetime.datetime.strptime("21/12/2008", "%d/%m/%Y").strftime("%Y-%m-%d")

The call to strptime() parses the first argument according to the format specified in the second, so those two need to match. Then you can call strftime() to format the result into the desired final format.

Answer from unwind on Stack Overflow
🌐
PYnative
pynative.com › home › python › python datetime › python datetime format using strftime()
Python DateTime Format using Strftime() – PYnative
May 6, 2022 - Now we can see more combinations with examples for a better understanding of the format codes that you can use to format dates in Python. The numerical format means to display the day, month, year, hours, minutes, seconds in numbers. like, 2021-07-07 12:19:47.864519 · from datetime import datetime # Get current Date x_date = datetime.now() print('Current Date:', x_date) # Represent Dates in numerical format print("dd-mm-yyyy HH:MM:SS:", x_date.strftime("%d-%m-%y %H:%M:%S")) print("dd-mm-yyyy:", x_date.strftime("%d-%m-%Y")) print("dd-mm-yy Format:", x_date.strftime("%d-%m-%y"))Code language: Python (python) Run
Discussions

arcpy - Python - Date notation to MM/DD/YYYY - Geographic Information Systems Stack Exchange
I'm a complete beginner trying to update some fields in an attribute table (i.e. an ArcMap Shapefile) using Python tool. My goal it to update the values from this column to another date notation. ... The dates need to be replaced to the following format MM/DD/YYY. More on gis.stackexchange.com
🌐 gis.stackexchange.com
November 19, 2016
DATE FORMAT HELP to DD-MM-YYYY
How do i change the date format to DD-MM-YYYY. I have date saved in my data table but when I run the code and see it in a repeating panel, its YYYY-MM-DD. Same goes for me picking a date from date picker. More on anvil.works
🌐 anvil.works
1
0
June 23, 2025
python - Pandas, convert datetime format mm/dd/yyyy to dd/mm/yyyy - Stack Overflow
Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal. ... Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... The default format of csv is dd/mm/yyyy... More on stackoverflow.com
🌐 stackoverflow.com
pandas - Python how to convert datetime.date to the YYYY-MM-DD? - Stack Overflow
I have a two million timestamp data. I am trying to find first and last date that to in the YYYY-MM-DD format so I can use them in saving file name. But, I found out that np.unique(df.index) is fas... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/learnpython › how can i convert yyyy-mm-dd to m/d/yyyy
r/learnpython on Reddit: How can I convert YYYY-MM-DD to M/D/YYYY
July 12, 2021 -

Hello all,

I have an excel file with about 100 columns, and 30 or so are dates, I would like to convert all the date formats

from:

 YYYY-MM-DD 

to

  M/D/YYYY

I was able to change it to MM/DD/YYYY using the following code

def fmt(input_dt):
if isnull(input_dt):
    return ""
else:
    return input_dt.strftime("%m/%d/%Y")

for col in df.columns:
if df[col].dtype == 'datetime64[ns]':
    df[col] = df[col].apply(fmt)

but that gives me

MM/DD/YYYY

I also need it to be datetime when exported back to excel.

I looked into the documentation

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

But it does not have M/D/YYYY any suggestions would be helpful. Thank you! Also if there is a more pythonic way to write it please let me know

🌐
Adverity
docs.adverity.com › reference › 2-enrich › python-expressions-for-managing-dates › converting-dates-from-one-format-in-to-another.html
Converting dates from one format to another — Adverity Documentation documentation
This example uses the convertx transformation to overwrite the original dates with the converted dates. The entered Python expression looks at a column called Dates and converts the dates from MM/DD/YYYY to DD-MM-YYYY.
🌐
GeeksforGeeks
geeksforgeeks.org › python › convert-datetime-string-to-yyyy-mm-dd-hhmmss-format-in-python
Convert datetime string to YYYY-MM-DD-HH:MM:SS format in Python - GeeksforGeeks
December 19, 2022 - # import datetime module from datetime import datetime # consider date in string format my_date = "30-May-2020-15:59:02" # convert datetime string into date,month,day and # hours:minutes:and seconds format using strptime d = datetime.strptime(my_date, "%d-%b-%Y-%H:%M:%S") # convert datetime format into %Y-%m-%d-%H:%M:%S # format using strftime print(d.strftime("%Y-%m-%d-%H:%M:%S")) ... # import datetime module from datetime import datetime # consider date in string format my_date = "30-May-2020-15:59:02" # convert datetime string into date,month,day # and hours:minutes:and seconds format using
🌐
Programiz
programiz.com › python-programming › datetime
Python datetime (With Examples)
Take a date string as an input and return it in 'mm-dd-yyyy' format.
Find elsewhere
🌐
Stack Abuse
stackabuse.com › bytes › getting-todays-date-in-yyyy-mm-dd-in-python
Getting Today's Date in YYYY-MM-DD in Python
September 20, 2023 - One alternative is using the time module, another built-in Python module for dealing with time. Here's how you can use it to get today's date: import time today = time.strftime("%Y-%m-%d") print(today) # Output: 2023-09-19 · When you run this code, you'll get the current date output in the 'YYYY-MM-DD' format.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › python › python get today’s current date and time
Python Get Today's Current Date and Time - nixCraft
August 6, 2024 - time.strftime(format) ## 24 hour format ## time.strftime("%H:%M:%S") ## 12 hour format ## time.strftime("%I:%M:%S") Here is another example done using datetime module: #!/usr/bin/python3 from datetime import date today = date.today() fdate = date.today().strftime('%d/%m/%Y') print("Today's current date is -", today) print("Date in dd/mm/YYYY format -", fdate)
🌐
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).
Top answer
1 of 3
16

You can use the parse_dates and dayfirst arguments of pd.read_csv, see: the docs for read_csv()

df = pd.read_csv('myfile.csv', parse_dates=['Date'], dayfirst=True)

This will read the Date column as datetime values, correctly taking the first part of the date input as the day. Note that in general you will want your dates to be stored as datetime objects.

Then, if you need to output the dates as a string you can call dt.strftime():

df['Date'].dt.strftime('%d/%m/%Y')
2 of 3
1

When I use again this: df['Date'] = pd.to_datetime(df['Date']), it gets back to the previous format.

No, you cannot simultaneously have the string format of your choice and keep your series of type datetime. As remarked here:

datetime series are stored internally as integers. Any human-readable date representation is just that, a representation, not the underlying integer. To access your custom formatting, you can use methods available in Pandas. You can even store such a text representation in a pd.Series variable:

formatted_dates = df['datetime'].dt.strftime('%m/%d/%Y')

The dtype of formatted_dates will be object, which indicates that the elements of your series point to arbitrary Python times. In this case, those arbitrary types happen to be all strings.

Lastly, I strongly recommend you do not convert a datetime series to strings until the very last step in your workflow. This is because as soon as you do so, you will no longer be able to use efficient, vectorised operations on such a series.

🌐
Finxter
blog.finxter.com › home › learn python blog › 5 best ways to format dates in python to yyyy-mm-dd
5 Best Ways to Format Dates in Python to YYYY-MM-DD - Be on the Right Side of Change
February 27, 2024 - One of the most common and straightforward methods to format dates in Python is using the datetime.strftime() function from the datetime module. This method enables you to represent a datetime object as a string in the “YYYY-MM-DD” format ...
🌐
DataCamp
datacamp.com › tutorial › converting-strings-datetime-objects
Convert String to DateTime in Python: Complete Guide with Examples | DataCamp
June 8, 2018 - Key format codes: %Y (4-digit year), %m (month), %d (day), %H (hour), %M (minute), %S (second). The datetime module, which comes in-built with Python, can be used whenever you need to work with dates, times, or time intervals for any application built using Python.
🌐
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: