0

I wanted to convert from 03FEB23 format to yyyy-mm-dd in python how can I do it?

Use the below code:

from pyspark.sql.functions import *
df=spark.createDataFrame([["1"]],["id"])
df.select(current_date().alias("current_date"), \
      date_format("03MAR23","yyyy-MMM-dd").alias("yyyy-MMM-dd")).show()
Umar.H
  • 22,559
  • 7
  • 39
  • 74
Gaurav Gangwar
  • 467
  • 3
  • 11
  • 24

2 Answers2

1
from datetime import datetime

date_str = '03FEB23'
date = datetime.strptime(date_str, '%d%b%y')
formatted_date = date.strftime('%Y-%m-%d')
print(formatted_date) # Output: 2023-02-03
Pranav Bilurkar
  • 955
  • 1
  • 9
  • 26
0

In pyspark:

Try with to_date function with ddMMMyy format.

Examples:

select to_date('03MAR23','ddMMMyy')
--2023-03-03

Dataframe API:

df=spark.createDataFrame([["03MAR23"]],["dt"])
df.withColumn("dt1", to_date(col("dt"),"ddMMMyy")).show()
#+-------+----------+
#|     dt|       dt1|
#+-------+----------+
#|03MAR23|2023-03-03|
#+-------+----------+
notNull
  • 30,258
  • 4
  • 35
  • 50