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: 5 months 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 9 years ago)
- Default Branch: main
- Last Pushed: 2025-02-17T19:39:06.000Z (11 months ago)
- Last Synced: 2025-03-05T22:44:45.031Z (11 months ago)
- Topics: c-sharp, datetime, datetimeoffset, extension-methods, months
- Language: C#
- Homepage:
- Size: 41 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).