convert format a datetime from 2023-01-01 00:00:00 to '11-may-2023' with pd.to_datetime in python

To convert a datetime format from '2023-01-01 00:00:00' to '11-may-2023' using pd.to_datetime in Python, you can follow these steps:

main.py
import pandas as pd

# Use pd.to_datetime to convert the datetime string to a pandas datetime object
datetime_obj = pd.to_datetime('2023-01-01 00:00:00')

# Use strftime to convert the datetime object to a formatted string
formatted_date = datetime_obj.strftime('%d-%b-%Y')

# Print the formatted date
print(formatted_date)
324 chars
11 lines

This will output:

main.py
11-May-2023
12 chars
2 lines

In the strftime function, %d represents the day with leading zeros, %b represents the abbreviated month name, and %Y represents the four-digit year.

related categories

gistlibby LogSnag