Mazil_tov998
Mazil_tov998

Reputation: 426

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

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.

Upvotes: 0

Views: 113

Answers (2)

shril
shril

Reputation: 152

You can use date_format function present in the pyspark library. For more information about date formats you can refer to Date Format Documentation.

Below is the code snippet to solve your usecase.

from pyspark.sql import functions as F

df = spark.createDataFrame([('2015-12-28 23:59:59',)], ['YDate'])
df = df.withColumn('YDate', F.date_format('YDate', 'dd/MM/yyy'))

Upvotes: 1

mck
mck

Reputation: 42392

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.

Upvotes: 1

Related Questions