Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading