Skip to main content
PYSPARK • LESSON 63

Extracting Day of Month with dayofmonth()

How do we extract the day of the month integer (1-31) from an order date?

Intermediate2 Minutes360 XP
🤔 THE QUESTION

How do we extract the day of the month integer (1-31) from an order date?

💡 WHAT IS IT?

dayofmonth() extracts the day of the month as an IntegerType from a date or timestamp Column.

🎯 WHAT IS IT USED FOR?

Daily reconciliation batches, payroll scheduling, and daily active user calculations.

💻 EXAMPLE
from pyspark.sql.functions import col, dayofmonth

df = df.withColumn("order_day", dayofmonth(col("order_date")))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Extracting Day of Month with dayofmonth().

  • Import dayofmonth function
  • Extract day integer (1-31)
  • Prepare daily batch partition key