Link to home
Start Free TrialLog in
Avatar of andre72
andre72

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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"));
}
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
Avatar of andre72
andre72

ASKER

Thanks