# Date Functions in PySpark

In this article, we will discuss various date functions in PySpark

First, let's create a sample data frame:

```python
from pyspark.sql import SparkSession
from pyspark.sql.functions import *

students = [{'id': '001', 'name': 'Achyut', 'dob': '1985-01-25'},
               {'id': '002', 'name': 'Chinmaya', 'dob': '1985-02-11'},
               {'id': '003', 'name': 'Ashmik', 'dob': '1985-02-02'},
               {'id': '004', 'name': 'Savar', 'dob': '2021-04-12'},
               {'id': '005', 'name': 'Trinabh', 'dob': '1985-01-25'}
               ]

students_df = spark.createDataFrame(students)
students_df.show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834316708/faab8931-44a8-4fa7-8f47-568976058847.png align="center")

# Date Functions

All the pyspark functions are present in the package from *pyspark.sql.functions*

**Reference :** [https://spark.apache.org/docs/2.4.2/api/python/pyspark.sql.html#module-pyspark.sql.functions](https://spark.apache.org/docs/2.4.2/api/python/pyspark.sql.html#module-pyspark.sql.functions)

```python
from pyspark.sql.functions import *
```

## year()

This method is used to return the year from the given date.

```python
students_df.select(
    col("dob"),
    year(col("dob")).alias("year")
).show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834841417/c8538cac-9270-4a65-baf2-87d1358ce911.png align="center")

## month()

This method is used to return the month from the given date.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834864337/8e4866ac-2b51-4d04-bc2d-f55130932b98.png align="center")

```python
students_df.select(
    col("dob"),
    month(col("dob")).alias("month")
).show()
```

## datediff()

This method is used to return the number of days between the current date and the given date.

```python
students_df.select(
    current_date(), 
    col("dob"),                     
    datediff(current_date(), col("dob")).alias("days_difference")
).show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834375671/fe7af176-c68d-4e6d-aaa0-465c6f9fc347.png align="center")

## months\_between ()

This method is used to return the number of months between the current date and given date.

```python
students_df.select(
    current_date(), 
    col("dob"), 
    months_between(current_date(), col("dob")).alias("months_difference")
).show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834403251/6d6cc7d9-1213-4791-9ed6-7f2e2d29f473.png align="center")

## add\_months

This method is used to add months to the given date.

```python
students_df.select(
    col("dob"),
    add_months(col("dob"),5).alias("after_5_months")
).show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834488802/e16def58-6efe-4f1b-95d2-142fb2101a74.png align="center")

## date\_add

This method is used to add days to the given date.

```python
students_df.select(
    col("dob"),
    date_add(col("dob"), 5).alias("after_5_days")
).show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834558909/d88fc43e-69b7-4af6-867f-3aff4a331143.png align="center")

## date\_sub()

This method is used to subtract days to the given date.

```python
students_df.select(
    col("dob"),
    date_sub(col("dob"), 5).alias("before_5_days")
).show()
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679834630123/a535e13f-f307-404c-86a6-0f422e05c2af.png align="center")

---

If you like my work and want to support me…

1. I share tips, tricks and insights on #softwareengineering, #dataengineering #cloud #ml on [LinkedIn](https://www.linkedin.com/in/naveen-pn/).
    
2. Do you want to connect with me I have started mentoring others for career and interviews at [𝐭𝐨𝐩𝐦𝐚𝐭𝐞.𝐢𝐨/𝐧𝐚𝐯𝐞𝐞𝐧𝐩𝐧](https://topmate.io/naveenpn)
