Solved

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

Posted on 2010-11-18
3
704 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

730 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