1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static class DateTimeExtension {
public static int getMonthsBetweenDates(this DateTime Date1, DateTime Date2) {
// Beide Daten in einer Liste speichern und sortieren
List<DateTime> period = new List<DateTime>() { Date1, Date2 };
period.Sort(DateTime.Compare);
// Monate zählen
int months;
for (months = 0; period[0].AddMonths(months + 1).CompareTo(period[1]) <= 0; months++) ;
return months;
}
}
|