ali raza

C#, .Net ,Asp.net and my thoughts

How to get month date view between two dates in c#

Scenario I got while developing some application. I required month and year view in drop down list between contract starting and ending date. Like “March 2009” “April 2009” until march 2011 so on.

I handled it as follow

DateTime _LoopDate = “1/1/2009”;
string month = string.Empty;
DateTime _EndDate = “1/1/2011”;

while (_LoopDate <= _EndDate)
{
System.Globalization.DateTimeFormatInfo d = new System.Globalization.DateTimeFormatInfo();
month = d.MonthNames[_LoopDate.Month – 1];

drpDate.Items.Add(new ListItem(month + ” ” + _LoopDate.Year.ToString(), _LoopDate.Month.ToString() + ” ” + _LoopDate.Year.ToString()));
//drpdate is dropdownlist
_LoopDate= _LoopDate.AddMonths(1);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: