Links

Convert time delta to months

Tags: #python #datetime #timedelta #calculate #date #time
Author: Florent Ravenel
Last update: 2023-05-22 (Created: 2023-05-22)
Description: This notebook convert the time delta between two dates to months.
References:

Input

Import libraries

from datetime import datetime

Setup Variables

  • start_date: First date
  • end_date: Second date
start_date = datetime(2022, 1, 15)
end_date = datetime(2023, 5, 22)

Model

Calculate time delta

Calculate the time delta between two dates using the timedelta function from the datetime library.
delta = end_date - start_date
months = round(delta.days / 30, 1)

Output

Display result

print(f"The time delta is {months} months.")