Q:

Get all the months between dates in MM/YYYY format

I want to get all the months with year in format MM/YYYY between two given dates.
Something like this:
Start date: 08/1/2012
End date: 03/1/2013

Result:
09/2012
10/2012
11/2012
12/2012
01/2013
02/2013
03/2013

This is the code I have but it doesn't work as I want.
for (int i = StartDate.Year; i <= EndDate.Year; i++)
{
  for (int j = StartDate.Month+1; j <= 12 ; j++)
  {
    Console.WriteLine(j.ToString("00") + "/" + i.ToString()));
  } 
}

Thank you in advance for your help.

A:

for 
(
    DateTime date = new DateTime(2012, 08, 01).AddMonths(1);
    date <= new DateTime(2013, 03, 01);
    date = date.AddMonths(1)
)
{
    Console.WriteLine(date.ToString("MM/yyyy"));
}

This would be more readable and robust if you write a separate enumerator for the dates, like so:
public static IEnumerable<DateTime> MonthsBetween(int startMonth, int startYear, int endMonth, int endYear)
{
    for
    (
        DateTime date = new DateTime(startYear, startMonth, 01).AddMonths(1);
        date <= new DateTime(endYear, endMonth, 01);
        date = date.AddMonths(1)
    )
    {
        yield return date;
    }
}

Which you could call like so:
foreach (var date in MonthsBetween(08, 2012, 03, 2013))
{
    Console.WriteLine(date.ToString("MM/yyyy"));
}

Note that this range excludes the first month, which from your example it seems you want.
If you don't want to skip the first month, remove the .AddMonths(1) from the line creating the start date.
(Personally, I'd prefer to include the starting month in the output, but that is not what you asked for.)

A:

There is the possibility that the day value of your startdate is greater than the day value of the enddate. In this case it is necessary to normalize the enddate to the end of the month
List<string> result = new List<string>();
DateTime startDate = new DateTime(2012, 1, 8);
DateTime endDate = new DateTime(2013, 1, 3);
DateTime temp = startDate;
endDate = new DateTime (endDate.Year, endDate.Month, DateTime.DaysInMonth(endDate.Year,endDate.Month));
while (temp <= endDate)
{
    Console.WriteLine((string.Format("{0}/{1}", temp.Month, temp.Year));
    temp = temp.AddMonth(1);
}

