Solved

Loop from one year to an other by every month

Posted on 2011-05-06
Medium Priority
314 Views
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
Question by:andre72

LVL 45

Accepted Solution

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)
{
...
}
0

LVL 13

Expert Comment

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

ID: 35705387
Hi,

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

Thanks
0

Author Closing Comment

ID: 35727647
Thanks
0

Featured Post

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
Course of the Month14 days, 8 hours left to enroll