Yatiac
Yatiac

Reputation: 2010

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.

Upvotes: 4

Views: 3553

Answers (6)

Matthew Watson
Matthew Watson

Reputation: 109547

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.)

Upvotes: 4

user2846553
user2846553

Reputation: 187

DateTime StartDate = Convert.ToDateTime("08/1/2012");
        DateTime EndDate = Convert.ToDateTime("03/1/2013");

        for (DateTime i = StartDate; i <= EndDate; i = i.AddMonths(1))
        {
            Response.Write(i.ToString("MM") + "/" + i.ToString("yyyy")+"<br />");
        }

Upvotes: 0

Dave Sexton
Dave Sexton

Reputation: 11188

Not tested but how about something like this:

public IEnumerable<String> EachMonth(DateTime start, DateTime end)
{
    for(var m = start.Date; m.Date <= end.Date; m = m.AddMonths(1))
        yield return m.ToString("MM/YYYY");
}

And then you could use it like this:

foreach (String month in EachMonth(StartDate, EndDate))

Upvotes: 0

Steve
Steve

Reputation: 216253

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);
}

Upvotes: 1

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

You can for loop with increasing month values with DateTime.AddMonths method like;

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

Output will be;

09.2012
10.2012
11.2012
12.2012
01.2013
02.2013
03.2013

Here a DEMO.

Upvotes: 1

hmnzr
hmnzr

Reputation: 1430

You can use this code, its more elegant:

for (DateTime date = StartDate; date <= EndDate; date = date.AddMonths(1))
    Console.WriteLine(date.ToString("MM/yyyy"));

Upvotes: 0

Related Questions