Converting date from string to date format with "/" as a date separator

Question:

I am trying to convert a date

"16/12/2021"

from a string to actual date format "DD-MM-YYYY" or "YYYY-MM-DD" in Pyspark
i have tried multiple ways of doing it including the below

df.select(to_date(datecol,"DD-MM-YYYY"))

And Also

date_format(to_timestamp(mydate,'16/12/2021'))

i am getting the same result for any method i use and the result is a Null value.

If i change the source date from the above to the below

"2022-12-16"

i get the conversion correct using any method.

any help with converting the date with "/" and dd/mm/yyyy format to a correct date format please ?

Thanks

Asked By: Mdahm

||

Answers:

For a general solution, you need a round trip from string to date, and then from date back to string.

date_format(to_date(datecol, 'dd/MM/yyyy'), 'yyyy-MM-dd')
Answered By: Tim Biegeleisen