Warm tip: This article is reproduced from serverfault.com, please click

How to make timestamp date column into preferred format dd/MM/yyyy?

发布于 2020-12-02 17:23:21

I have a column YDate in the form yyyy-MM-dd HH:mm:ss (timestamp type) but would like to convert it to dd/MM/yyyy.

I tried that;

df = df.withColumn('YDate',F.to_date(F.col('YDate'),'dd/MM/yyyy'))

but get yyyy-MM-dd.

How can I effectively do this.

Questioner
Mazil_tov998
Viewed
0
mck 2020-12-03 02:48:34

Use date_format instead:

df = df.withColumn('YDate',F.date_format(F.col('YDate'),'dd/MM/yyyy'))

to_date converts from the given format, while date_format converts into the given format.