Skip to main content

months_between

Returns number of months between dates date1 and date2. If date1 is later than date2, then the result is positive. A whole number is returned if both inputs have the same day of month or both are the last day of their respective months. Otherwise, the difference is calculated assuming 31 days per month. The result is rounded off to 8 digits unless roundOff is set to False.

For the corresponding Databricks SQL function, see months_between function.

Syntax

Python
from pyspark.databricks.sql import functions as dbf

dbf.months_between(date1=<date1>, date2=<date2>, roundOff=<roundOff>)

Parameters

Parameter

Type

Description

date1

pyspark.sql.Column or str

first date column.

date2

pyspark.sql.Column or str

second date column.

roundOff

bool, optional

whether to round (to 8 digits) the final value or not (default: True).

Returns

pyspark.sql.Column: number of months between two dates.

Examples

Python
from pyspark.databricks.sql import functions as dbf
df = spark.createDataFrame([('1997-02-28 10:30:00', '1996-10-30')], ['d1', 'd2'])
df.select('*', dbf.months_between(df.d1, df.d2)).show()
df.select('*', dbf.months_between('d2', 'd1')).show()
df.select('*', dbf.months_between('d1', df.d2, False)).show()