[Solved] in C#, a method who gives me a list of months and year between two dates [closed]


This function will do it. What it returns is a series of dates – the first day of each month which is part of the range.

public IEnumerable<DateTime> GetMonths(DateTime startDate, DateTime endDate)
{
    if(startDate > endDate) 
    {
        throw new ArgumentException(
            $"{nameof(startDate)} cannot be after {nameof(endDate)}");
    }
    startDate = new DateTime(startDate.Year, startDate.Month, 1);
    while (startDate <= endDate)
    {
        yield return startDate;
        startDate = startDate.AddMonths(1);
    }
}

Usage:

var months = GetMonths(startDate, endDate);

For example, if the the parameters are February 7, 2016 and April 2, 2016, it will return
February 1, 2016
March 1, 2016
April 1, 2016

solved in C#, a method who gives me a list of months and year between two dates [closed]