Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/markwhitaker/monthdiff
- Owner: markwhitaker
- License: mit
- Created: 2017-04-15T15:54:33.000Z (almost 8 years ago)
- Default Branch: main
- Last Pushed: 2025-01-13T08:24:54.000Z (5 days ago)
- Last Synced: 2025-01-13T09:32:19.051Z (5 days ago)
- Topics: c-sharp, datetime, datetimeoffset, extension-methods, months
- Language: C#
- Homepage:
- Size: 38.1 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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).