Visual C# Developer Center > Visual C# Forums > Visual C# General > How to string together a date with Month, Day, Year
Ask a questionAsk a question
 

AnswerHow to string together a date with Month, Day, Year

  • Friday, November 06, 2009 2:07 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Ok, I'm not quite sure how to go about this. Here's the scenario.

    1) On a web form we have 3 dropdowns: Month, Day, and Year

    2) Year is always optional (not required)

    3) If the customer enters month and day, and not year, we want to default the year to 1900

    4) If the customer does enter all 3, I need to piece together a DateTime to represent that. Either way, the year is going to have something...either a valid year or 1900 if the user did not select year.

    So in my code-behind, I'm not quite sure how to set all this up. Ultimately I need to form that date so I can update the SQL 2008 Date datatype once I send down the date to my DL update function.

    So I created a DataTime variable in my code-behind method that picks up the values that the user has selected in each dropdown. However I guess there's no setter on DateTime.Year and so fourth. So I can't just do DateTime.year = "1900" or something to that effect.


    C# Web Developer

Answers

  • Friday, November 06, 2009 2:12 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    No, DateTime is immutable, which means you'll have to construct an entirely new DateTime object to deal with it.

    Here's how you would do it:

    int day = int.Parse(dayDropDown.SelectedItem.Text);
    int month = int.Parse(monthDropDown.SelectedItem.Text);
    int year;
    if (!int.TryParse(yearDropDown.SelectedItem.Text, out year))
       year = 1900;

    DateTime result = new DateTime(year, month, day);

    Obviously, you might have different names for your dropdowns, and you might use SelectedValue if you're using databinding or something, but the general algorithm is going to be the same.  Parse out all of your values, use the three lines strarting with "int year;" to create a default value for year, and then construct your DateTime object.
     
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked As Answer byNoEgo Friday, November 06, 2009 2:20 PM
    •  

All Replies

  • Friday, November 06, 2009 2:12 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    No, DateTime is immutable, which means you'll have to construct an entirely new DateTime object to deal with it.

    Here's how you would do it:

    int day = int.Parse(dayDropDown.SelectedItem.Text);
    int month = int.Parse(monthDropDown.SelectedItem.Text);
    int year;
    if (!int.TryParse(yearDropDown.SelectedItem.Text, out year))
       year = 1900;

    DateTime result = new DateTime(year, month, day);

    Obviously, you might have different names for your dropdowns, and you might use SelectedValue if you're using databinding or something, but the general algorithm is going to be the same.  Parse out all of your values, use the three lines strarting with "int year;" to create a default value for year, and then construct your DateTime object.
     
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked As Answer byNoEgo Friday, November 06, 2009 2:20 PM
    •  
  • Friday, November 06, 2009 2:20 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    thanks did not know you can set those in the DateTime constructor!
    C# Web Developer
  • Friday, November 06, 2009 3:04 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hmmm, one more thing.  If data is optional on the web form...how can I default month and day?  I tried zero but obviously DateTime doesn't like that.
    C# Web Developer
  • Friday, November 06, 2009 3:08 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    You'll have to use the same general thing as you're currently using for year, just repeat that algorithm for month and day. 

    Either that or only give the user options that work, which is going to be rough ultimately...

    What if they pick February 30?

    You'll probably need to use some Javascript to restrict the input a little bit for the shorter months (by removing "31" or "31", "30", and "29" for February), so that doesn't happen, and only give the user viable options.  But now that we're getting into Javascript, we're off-topic for the forum.  Try http://forums.asp.net/

    If you really need a default, though, just use 1.  You can always use Jan 1 as a default.


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 3:12 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    >>You'll probably need to use some Javascript to restrict the input a little bit for the shorter months

    yea, I've got that in place already :)

    >>>If you really need a default, though, just use 1.  You can always use Jan 1 as a default.

    here's the problem.  A customer could have a birthday of 1/1 however I guess no matter what we're gonna check for 1900 anyway for the year regardless.   Yea, that would work.  I'd just need to make sure any logic where we're checking month (because we're going to be showing the customer some stuff on their profile during the month of their birthday IF they've specified their birthday in the past...that we also check for 1900.  I'd like to stay away from having any developers having to remember this though because if I check for only month, and forget to check that year is valid, I could be showing the customer logic that I don't indent to show.  Because 1900 means that the customer did not update their birthday and should not be seeing these special benefits)

    OK, here's my code:

                int month = 0;
                int day = 0;
                int year = 1900;

                if (ddlBirthMonth.SelectedIndex == 0 && ddlBirthYear.SelectedIndex == 0)
                    return;

                if (ddlBirthDay.SelectedIndex > 0)
                    day = ddlBirthDay.SelectedIndex;

                if (ddlBirthMonth.SelectedIndex > 0)
                    month = ddlBirthMonth.SelectedIndex;

                if (ddlBirthYear.SelectedIndex > 0)
                    Int32.TryParse(ddlBirthYear.SelectedValue, out year);

                DateTime birthDate = new DateTime(year, month, day);
                customer.BirthDate = birthDate;

    so, that's how I'm doing it.  I guess we could just default to MinDate? if nothing's selected for the overall birth date fields on the form.
    C# Web Developer
    • Edited byNoEgo Friday, November 06, 2009 3:15 PM
    •  
  • Friday, November 06, 2009 3:14 PMDavid M MortonMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Just declare Month and Day as 1 at the top of your code.


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • Friday, November 06, 2009 3:16 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I just updated my previous post.  See my concern there with a valid month/day default.  Maybe there's just no way around that.
    C# Web Developer
  • Friday, November 06, 2009 3:53 PMNoEgo Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    thanks for the pointers.

    C# Web Developer