pyspark.sql.functions.months_between(date1: ColumnOrName, date2: ColumnOrName, roundOff: bool = True) → pyspark.sql.column.Column

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.


>>> df = spark.createDataFrame([('1997-02-28 10:30:00', '1996-10-30')], ['date1', 'date2'])
>>>, df.date2).alias('months')).collect()
>>>, df.date2, False).alias('months')).collect()