pyspark.sql.functions.date_format

pyspark.sql.functions.date_format(date: ColumnOrName, format: str) → pyspark.sql.column.Column[source]

Converts a date/timestamp/string to a value of string in the format specified by the date format given by the second argument.

A pattern could be for instance dd.MM.yyyy and could return a string like ‘18.03.1993’. All pattern letters of datetime pattern. can be used.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
dateColumn or str

input column of values to format.

format: str

format to use to represent datetime values.

Returns
Column

string value representing formatted datetime.

Notes

Whenever possible, use specialized functions like year.

Examples

>>> df = spark.createDataFrame([('2015-04-08',)], ['dt'])
>>> df.select(date_format('dt', 'MM/dd/yyy').alias('date')).collect()
[Row(date='04/08/2015')]