Solved

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

Posted on 2010-11-18
3
696 Views
Last Modified: 2013-12-14
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

0
Comment
Question by:zattz
3 Comments
 
LVL 19

Accepted Solution

by:
Bardobrave earned 250 total points
ID: 34163898
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
 
LVL 18

Assisted Solution

by:lludden
lludden earned 250 total points
ID: 34163933
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
 

Author Comment

by:zattz
ID: 34164290
Thanks.
I didnt realise you could loop through dates!
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

829 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