# of Months between two dates
steve+comp.lang.python at pearwood.info
Fri Apr 6 02:16:37 EDT 2018
On Thu, 05 Apr 2018 22:03:07 -0600, Jason Friedman wrote:
>> > > I've written a function to return the months between date1 and
>> > > date2
>> > > I'd like to know if anyone is aware of anything in the standard
>> > > library to do the same? For bonus points, does anyone know if
>> > > postgres can do the same (we use a lot of date/time funcitons in
>> > > postgres, already, but didn't see this problem addressed).
>> > It's probably better to write the function yourself according to what
>> > makes sense in your use-case, and document its behaviour clearly.
> I suggest using the dateutil module (
> https://pypi.python.org/pypi/python-dateutil) before writing your own.
I'm not seeing a "months between" function in dateutil. Have I missed
The question of how many months are between two dates is hard to answer.
- between Jan 1 and Feb 1 is clearly one month (31 days);
- unless you think all months should be 30 days, in which case
its one month and one day;
- giving one month from Jan 1 as Jan 31, which seems weird;
- either way, the period between Feb 1 and Mar 1 is only four
weeks and so obviously less than one month;
- unless you think a month is four weeks precisely;
- in which case it is one month between Jan 1 and Jan 29;
- one month forward from Jun 30 is clearly Jul 30;
- but one month back from Jul 31 is either Jun 30 or Jul 1;
- instead of counting days, with all the difficulty that
causes, we could just count how many times the month
- in which case, Jan 31 to Feb 1 is one month.
I wish the calendar designers had made the year 13 months of exactly 4
weeks each, with one intercalary day left over (two in leap years). Half
a year becomes exactly 6 months and two weeks; a quarter of a year
becomes three months and one week. A third is a bit more than four weeks.
Date calculations would be much simpler, with only one (or two in leap
years) special case, the intercalary day, instead of 36.
More information about the Python-list