Home > Net >  Convert date time to date format
Convert date time to date format

Time:10-30

I have a column with datetime "2021-10-22 00:00:00" and I want to convert it to date with format "10/22/2021"

Thanks

CodePudding user response:

You can convert string to a datetime object with .strptime() method , then create new datetime string with .strftime() method and new pattern.

from datetime import datetime

date_time_str = "2021-10-22 00:00:00"
main_pattern = '%Y-%m-%d %H:%M:%S'
target_pattern = '%m/%d/%Y'

date_time_obj = datetime.strptime(date_time_str, main_pattern)
new_date_time = date_time_obj.strftime(target_pattern)

print (new_date_time)
#output:
#10/22/2021
  • Related