Populating a List box with recent dates

D

djjohnst

I am having a interesting issue. I work for a University's Career
Services department. We collect data from recent grads. I am
recreating the online survey we use. I am trying to populate the
graduation date with 4 dates. May **, June **, August **, December **.
The "**" represents the year of graduation. To prevent having to
constantly update this survey I'd like to dynamically create this
values. So at page load I want it to check the server time and only
show the months from the past year. IE since Today is June 07 the
values would read June 07, May 07, December 06, August 06. Then in
August It would read August 07, June 07, May 07, December 06. Etc... I
am trying to think of a clever way to do this and just can not do
this. Anyone have an idea???
 
M

Mark Rae

Anyone have an idea???

List<DateTime> lstDates = new List<DateTime>();
DateTime dtmStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
while (lstDates.Count < 4)
{
if (dtmStart.Month == 5
|| dtmStart.Month == 6
|| dtmStart.Month == 8
|| dtmStart.Month == 12)
{
lstDates.Add(dtmStart);
}
dtmStart = dtmStart.AddMonths(-1);
}
 
D

Dunc

How about something like...

DateTime dt = DateTime.Today();

for (int iLoop = 0; iLoop < 4; iLoop++)
{
ddlMyDropDown.Items.Add(new ListItem(String.Format("{0:MMM yy}",
dt)));
dt = dt.DateAdd("MM", -3, dt);
}

Dunc
http://www.fluidfoundation.com
 
A

Alexey Smirnov

List<DateTime> lstDates = new List<DateTime>();
DateTime dtmStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
while (lstDates.Count < 4)
{
if (dtmStart.Month == 5
|| dtmStart.Month == 6
|| dtmStart.Month == 8
|| dtmStart.Month == 12)
{
lstDates.Add(dtmStart);
}
dtmStart = dtmStart.AddMonths(-1);

}

--http://www.markrae.net

Well done, Mark!
 
M

Mark Rae

DateTime dt = DateTime.Today();

for (int iLoop = 0; iLoop < 4; iLoop++)
{
ddlMyDropDown.Items.Add(new ListItem(String.Format("{0:MMM yy}",
dt)));
dt = dt.DateAdd("MM", -3, dt);
}

Suppose you start today, what are the four dates which your code will add to
the DropDownList...?
 
D

David Longnecker

Just a bit of modification to Mark's code to output it as djjohnst was looking
for, in the "June XX" format):

List<String> lstDates = new List<String>();
DateTime dtmStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month,
1);
while (lstDates.Count < 4)
{
if (dtmStart.Month == 5
|| dtmStart.Month == 6
|| dtmStart.Month == 8
|| dtmStart.Month == 12)
{
lstDates.Add(dtmStart.ToString("MMMM yy"));
}
dtmStart = dtmStart.AddMonths(-1);
}

ddlGradDates.DataSource = lstDates;
ddlGradDates.DataBind();

- Converted it from a list collection of DateTimes to Strings; maybe a KVP
to keep the "data" and presentation apart may be a solution if the two need
to be different.

- Added the MMMM yy to output the long Month name and short year.

HTH.

-dl
 
M

Mark Rae

Just a bit of modification to Mark's code to output it as djjohnst was
looking for, in the "June XX" format):

True enough - I took the final formatting "as read", and assumed that the
it was the actual date generation that was causing the OP problems... :)
 
D

djjohnst

As of today i would want it to display the following options
June 07
May 07
Dec 06
August 06
 
D

djjohnst

Forgive me. I am really new to ASP.net. Where would i put that code? I
tried in the Head section and it did not work.
 
M

Mark Rae

Forgive me. I am really new to ASP.net. Where would i put that code? I
tried in the Head section and it did not work.

The code I gave you is C#, which runs server-side so it can't go in your
page's header section...

Are you using in-line server-side code or code-behind...?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top