get year of current date

arkiboys 9,686 Reputation points
2022-04-21T22:30:51.257+00:00

Do you see why I do not get the results for the date_format and dayofmonth?

from pyspark.sql.functions import dayofmonth
2
from pyspark.sql.functions import year
3
from pyspark.sql.functions import month
4
from pyspark.sql.functions import to_timestamp,date_format
5
from pyspark.sql.functions import current_timestamp
6

7

current_timestamp()

8
date_format(current_timestamp(), 'M')
9
dayofmonth(current_timestamp())

Out[7]: Column<b'dayofmonth(current_timestamp())'>

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
2,059 questions
0 comments No comments
{count} votes

Accepted answer
  1. ShaikMaheer-MSFT 38,326 Reputation points Microsoft Employee
    2022-04-22T05:55:46.22+00:00

    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.
    195358-image.png

    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.


0 additional answers

Sort by: Most helpful