public static int MonthsBetween(
this DateTimeOffset firstDate,
DateTimeOffset secondDate
)
The parameters firstDate and secondDate may be specified in any order. If firstDate is less than or equal to secondDate, a positive number will be returned. If firstDate is larger, a negative number will be returned.
First date | Second date | Result |
---|---|---|
2001-01-01 | 2001-03-31 | 2 |
2001-01-31 | 2001-03-01 | 2 |
2001-01-01 | 2001-01-31 | 0 |
2001-01-31 | 2001-01-01 | 0 |
2001-01-01 | 2000-12-31 | -1 |
var firstDate = new DateTimeOffset(2012, 1, 1, 0, 0, 0, TimeSpan.Zero);
var secondDate = new DateTimeOffset(2010, 1, 1, 0, 0, 0, TimeSpan.Zero);
? firstDate.MonthsBetween(secondDate);
// Result: -24