?
Solved

Loop from one year to an other by every month

Posted on 2011-05-06
4
Medium Priority
?
314 Views
Last Modified: 2013-12-16
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
0
Comment
Question by:andre72
4 Comments
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 35705322
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
 
LVL 13

Expert Comment

by:iHadi
ID: 35705366
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
 
LVL 16

Expert Comment

by:Imran Javed Zia
ID: 35705387
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
 

Author Closing Comment

by:andre72
ID: 35727647
Thanks
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question