Populating a List box with recent dates

Discussion in 'ASP .Net' started by djjohnst, Jun 1, 2007.

  1. djjohnst

    djjohnst Guest

    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???
     
    djjohnst, Jun 1, 2007
    #1
    1. Advertising

  2. djjohnst

    Mark Rae Guest

    "djjohnst" <> wrote in message
    news:...

    > 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);
    }


    --
    http://www.markrae.net
     
    Mark Rae, Jun 1, 2007
    #2
    1. Advertising

  3. djjohnst

    Dunc Guest

    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

    On 1 Jun, 14:08, djjohnst <> wrote:
    > 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???
     
    Dunc, Jun 1, 2007
    #3
  4. On Jun 1, 3:25 pm, "Mark Rae" <> wrote:
    > "djjohnst" <> wrote in message
    >
    > news:...
    >
    > > 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);
    >
    > }
    >
    > --http://www.markrae.net


    Well done, Mark!
     
    Alexey Smirnov, Jun 1, 2007
    #4
  5. djjohnst

    Mark Rae Guest

    "Dunc" <> wrote in message
    news:...

    > 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...?


    --
    http://www.markrae.net
     
    Mark Rae, Jun 1, 2007
    #5
  6. djjohnst

    Mark Rae Guest

    Mark Rae, Jun 1, 2007
    #6
  7. 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

    ---
    David R. Longnecker
    Web Developer
    http://blog.tiredstudent.com

    > "djjohnst" <> wrote in message
    > news:...
    >
    >> 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);
    > }
     
    David Longnecker, Jun 1, 2007
    #7
  8. djjohnst

    Mark Rae Guest

    "David Longnecker" <> wrote in message
    news:...

    > 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... :)


    --
    http://www.markrae.net
     
    Mark Rae, Jun 1, 2007
    #8
  9. djjohnst

    djjohnst Guest

    As of today i would want it to display the following options
    June 07
    May 07
    Dec 06
    August 06
     
    djjohnst, Jun 1, 2007
    #9
  10. djjohnst

    Mark Rae Guest

    "djjohnst" <> wrote in message
    news:...
    > As of today i would want it to display the following options
    > June 07
    > May 07
    > Dec 06
    > August 06



    Yes, I know...


    --
    http://www.markrae.net
     
    Mark Rae, Jun 1, 2007
    #10
  11. djjohnst

    djjohnst Guest

    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.
     
    djjohnst, Jun 1, 2007
    #11
  12. djjohnst

    Mark Rae Guest

    "djjohnst" <> wrote in message
    news:...

    > 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...?


    --
    http://www.markrae.net
     
    Mark Rae, Jun 1, 2007
    #12
  13. djjohnst

    djjohnst Guest

    in-line. Do you think it would be better to do code behind?
     
    djjohnst, Jun 1, 2007
    #13
  14. djjohnst

    Mark Rae Guest

    "djjohnst" <> wrote in message
    news:...

    > in-line.


    OK.

    > Do you think it would be better to do code behind?


    Wouldn't make the slightest difference...


    --
    http://www.markrae.net
     
    Mark Rae, Jun 1, 2007
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gerry
    Replies:
    0
    Views:
    568
    gerry
    Apr 24, 2004
  2. Ken McCrory

    Text box losing most recent Text value

    Ken McCrory, Mar 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    465
    Patrick Olurotimi Ige
    Mar 10, 2005
  3. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    904
    Rob Schieber
    Sep 30, 2005
  4. Lee Trotter

    Problem Populating a List Box

    Lee Trotter, Jun 24, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    113
    Lee Trotter
    Jun 24, 2004
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    230
    Mark Schupp
    Aug 9, 2004
Loading...

Share This Page