public static int MonthsBetween(
this DateTime firstDate,
DateTime 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 DateTime(2002, 1, 1);
var secondDate = new DateTime(2003, 1, 1);
? firstDate.MonthsBetween(secondDate);
// Result: 12