Hi @arkiboys ,
Thank you for posting query in Microsoft Q&A Platform.
As per my understanding you are trying to get year from current date in pyspark. Please correct me if I am wrong.
We should consider using
date_format(current_timestamp(),'y')
in pyspark to get year from current date.
Please check below screenshot.
Code used in above screenshot available below.
from pyspark.sql.functions import dayofmonth
from pyspark.sql.functions import year
from pyspark.sql.functions import month
from pyspark.sql.functions import to_timestamp,date_format
from pyspark.sql.functions import current_timestamp
df = spark.createDataFrame([(1,'maheer'),(2,'pradeep')],schema=['id','name'])
display(
df.withColumn('currentTime',current_timestamp())
.withColumn('currentMonth',date_format(current_timestamp(),'M'))
.withColumn('currentday',dayofmonth(current_timestamp()))
.withColumn('currentYear',date_format(current_timestamp(),'y'))
)
Hope this helps. Please let us know if any further queries.
---------------
Please consider hitting Accept Answer
button. Accepted answers help community as well.