Loop from one year to an other by every month

Hi,

I need to loop from one moth of a year to an other moth of an other year, eg.:
Startmonth: 2010-06-01
Endmonth: 2011-03-01

So loop:
2010-06
...
2010-11
2010-12
2011-01
2011-02
...

How can I do a loop like this?

Thanks

Andre
andre72Asked:
Who is Participating?
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
DateTime dt = new DateTime(2010, 6, 1, 0, 0, 0);
DateTime dtEnd = new DateTime(2011, 3, 1, 0, 0, 0);

while(dt < dtEnd)
{
...
  dt = dt.AddMonth(1);
}
0
 
iHadiCommented:
DateTime startDate = DateTime.Parse("6/6/2010");
DateTime endDate = DateTime.Parse("8/8/2010");

for (DateTime i = startDate; i <= endDate; i = i.AddMonths(1))
{
    MessageBox.Show(i.ToString("MM/yyyy"));
}
0
 
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
Hi,

Please try following code:

 
DateTime Startmonth = new DateTime(2010,6,1);
            DateTime Endmonth = new DateTime(2011, 3, 1);

            DateTime dt = new DateTime(Startmonth.Year, Startmonth.Month, 1); // also equals to Startmonth

            while (dt < Endmonth)
            {
                MessageBox.Show(dt.ToString("yyyy-MM"));
                dt = dt.AddMonths(1);
            }

Open in new window


Thanks
0
 
andre72Author Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.