c# get list of months between two DateTimes, formatted as YYYYmm

Hi

Can somebody show me a smart way to get a list of months between two DateTime values and format the results such as:

200811
200812
200901
200902
200903

Thanks

zattzAsked:
Who is Participating?
 
BardobraveConnect With a Mentor Commented:
You can use a loop from one date to another, increasing in each step first date on a month.

As you're working with date objects you can directly response it's year and month.
0
 
lluddenConnect With a Mentor Commented:
System.DateTime StartDate = 1/1/2005 12:00:00 AM;
System.DateTime EndDate = 1/1/2009 12:00:00 AM;

while (StartDate < EndDate) {
	FileSystem.WriteLine(Strings.Format(StartDate, "yyyyMM"));
	StartDate = DateAndTime.DateAdd(DateInterval.Month, 1, StartDate);
}

Open in new window

0
 
zattzAuthor Commented:
Thanks.
I didnt realise you could loop through dates!
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.