Try the following:
static IEnumerable<DateTime> monthsBetween(DateTime d0, DateTime d1) { return Enumerable.Range(0, (d1.Year - d0.Year) * 12 + (d1.Month - d0.Month + 1)) .Select(m => new DateTime(d0.Year, d0.Month, 1).AddMonths(m)); }
This includes both the start month and the end month. This finds how many months are, and then creates a new DateTime based on d0 year and month. This means that months are like yyyy-MM-01 . If you want it to include the time and day d0 , you can replace new DateTime(d0.Year, d0.Month, 1).AddMonths(m) with d0.AddMonths(m) .
I see that you need an array, in this case you just use monthsBetween(..., ...).ToArray() or put .ToArray() inside the method.
Lasse espeholt
source share