Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/markwhitaker/monthdiff

Simple C# extension methods to calculate the difference in months between two dates
https://github.com/markwhitaker/monthdiff

c-sharp datetime datetimeoffset extension-methods months

Last synced: 2 days ago
JSON representation

Simple C# extension methods to calculate the difference in months between two dates

Awesome Lists containing this project

README

        

# MonthDiff

A very simple pair of extension methods on `DateTime` and `DateTimeOffset` to calculate the difference in months between two dates.

I wanted it to work exactly like a `TotalMonths` property on `TimeSpan` would work, i.e. return the count of complete months between two dates, ignoring any partial months. Because it's based on `DateTime.AddMonths()` it respects different month lengths and returns what a human would understand as a period of months. Unfortunately it can't be implemented as an extension method on `TimeSpan` because that doesn't retain knowledge of the actual dates used, and for months they're important.

Originally posted at [StackOverflow](http://stackoverflow.com/questions/1525990/calculating-the-difference-in-months-between-two-dates/25136172#25136172).