Find the number of months in Python for two days

Asked 2 years ago, Updated 2 years ago, 66 views

I'd like to find the number of months between the two dates. If I find the difference between date formats, it only comes out as days... Is there a separate method to get the number of months?

```python 3

from datetime import date, timedelta a = date(1998,5, 1) b = date(2017, 12, 31) b - a

python date

2022-09-22 20:20

1 Answers

Dateutil is available.

>>> from dateutil import rrule
>>> from datetime import date
>>> diff_months_list = list(rrule.rrule(rrule.MONTHLY, dtstart=date(2016, 10, 1), until=date(2017, 5, 15)))
>>> print(diff_months_list)
[datetime.datetime(2016, 10, 1, 0, 0), datetime.datetime(2016, 11, 1, 0, 0), datetime.datetime(2016, 12, 1, 0, 0), datetime.datetime(2017, 1, 1, 0, 0), datetime.datetime(2017, 2, 1, 0, 0), datetime.datetime(2017, 3, 1, 0, 0), datetime.datetime(2017, 4, 1, 0, 0), datetime.datetime(2017, 5, 1, 0, 0)]
>>> print(len(diff_months_list))
8


2022-09-22 20:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.