1

The program needs to find the youngest (or oldest) person within a list. The birthday is actually a string. I converted the dates using Convert.ToDateTime(), which is working fine. However, in a few cases, the input is just a year, without the day and the month, e.g. "1945". The converter fails.

My questions:

1) what checker can I add in order to prevent such a run-time failure?

2) How can I convert it to date, without overriding strings that include the day and the month? the code is:

DateTime curr_bday = Convert.ToDateTime(p.Birthday);

here is the full routine:

public string the_youngest(Persons _p2)
{
    DateTime youngAge = Convert.ToDateTime("01/01/1005");

    DateTime curr_bday;
    string to_ret = "";

    foreach (var p in _p2.persons)
    {
        curr_bday = Convert.ToDateTime(p.Birthday);
        if (curr_bday < youngAge)
        {
            youngAge = curr_bday;
            to_ret = p.Name + " : " + p.Birthday;
        } 
    }
    return (to_ret);
} 
4
  • Where are these strings coming from, and can you give us a few examples? Commented Mar 21, 2019 at 18:17
  • Test to see if the string is an integer with int.TryParse. If it is, initialize your birthday variable with a newly constructed DateTime where you specify only the year. Otherwise, parse the string as a date time Commented Mar 21, 2019 at 18:17
  • The strings come from a parser. That's a given input. Commented Mar 21, 2019 at 18:23
  • @ Flydog57 : it looks like it is working well. I had a bug in the compare part (bigger birhday means younger person) Thank you ! Commented Mar 21, 2019 at 18:43

3 Answers 3

1

Explore the TryParse function. it works with dates, integers, and decimals.

DateTime birthdate;
if(DateTime.TryParse(p.Birthday, out birthdate))
{
  //full date here successfully converted
  //continue
}
else
{
   birthdate = new DateTime(Int32.Parse(p.Birthday), 1, 1); //1 Jan by default
}
Sign up to request clarification or add additional context in comments.

Comments

1

Using TryParse all over is your best bet:

DateTime bDay;
if (int.TryParse(dateString, out var birthYear))
{
    dateString = new DateTime(birthYear, 7, 4); //born on the 4th of July
}
else if (!DateTime.TryParse( dateString, out bDay))
{
    //let the user know there's a problem
}

1 Comment

most stable and concise solution here, have an upvote! ;)
0

Assuming the only two possible formats are {year} and {valid datetime} string then you can do

DateTime d;
if (p.Birthday.Length <= 4)
{
     int year = int.Parse(p.Birthday);
     d = new DateTime(year, 1, 1);
}
else
{
     d = DateTime.Parse(p.Birthday)
}

This will work for another 8000 years. However if there are other special case then you need to handle those separately.

1 Comment

You are better off using int.TryParse in the if statement. What about "3/21" (today). It's four characters, and I believe it would be a valid date in a US copy of Excel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.