I needed to count the actual whole calendar months between two dates with no assumptions about the nature of anything between them. That means no knowledge of days in each month (which vary even before considering leap years), seconds in each day (which may vary with time zones or leap seconds), or even months in each year. Adding magic numbers, or worse, enumerating lots of Gregorian calendar eccentricities, are nasty code smells.
Unsatisfied with solutions like SQL Server's DATEDIFF()
that naively counts datepart boundaries and Oracle's MONTHS_BETWEEN()
that naively thinks every month is 31 days long, I checked StackOverflow and other usual resources, but nobody had a sane solution. Some authors suggested calendar tables (✋), and others suggested CLR functions (