i was working on calculate the birthdate when user select the date,month,year by combobox. the age should display on textbox. i have wrote some code but it show the
error messsage.

can any one solve this please

public static int CalculateAge(DateTime birthDate)
           DateTime now = DateTime.Today; 
           int years = now.Year - birthDate.Year;
       (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))


            return years;

on button click event the following coding is working

int year = Convert.ToInt32(comboBox1.Text );

       int month = Convert.ToInt32(comboBox2.Text); // it contain month in string format

            int day = Convert.ToInt32(comboBox3.Text );

 [B]           DateTime birthDate = new DateTime(year, month, day);[/B]   // error    

            MessageBox.Show("Your Age is:" + Convert.ToString(CalculateAge(birthDate)));

error message = Year, Month, and Day parameters describe an un-representable DateTime.

Edited by virusisfound: n/a

6 Years
Discussion Span
Last Post by ddanbe
Featured Replies
  • 1
    ddanbe 2,525   6 Years Ago

    Besides the fact that a DateTimePicker is a lot more user friendly than a set of comboboxes, it has more to offer. Look [url=http://msdn.microsoft.com/en-us/library/system.windows.forms.datetimepicker.aspx]here[/url] for more info and a code example. Read More


What are the values that you are putting in the comboboxes? Rather than use Convert try using




and check the values before you put them into the DateTime constructor. I'll have to check if the DateTime constructor makes sure that the date is in a reasonable range.

Also, why not use a date/time picker instead of your own comboboxes? It gives you data in the right form already and you can use the TimeSpan object to get the difference between the dates.

Edited by happygeek: fixed formatting


This is a variation of your CalculateAge method.
I used the operator overloading features of DateTime, so I would not have to work with a huge boolean expression.
Feel free to use it or not.

public static int CalculateAge(DateTime birthDate)
            DateTime testDate = new DateTime(DateTime.Today.Year, birthDate.Month, birthDate.Day);
            int years = DateTime.Today.Year - birthDate.Year;
            if (testDate > DateTime.Today) //you still have not had your birthday this year
            return years;

I would strongly advise you to follow the advise of Jonsca: use a DateTimePicker.
It gives you a Value property wich is of type DateTime.


before using the combobox I was tried to code on DateTimePicker but i don't know how to perform coding on it . because in combobox i have seperate Text to calculate the year only I know it's wrong method to code but I realy don't have any idea about DateTimePicker property.

if u don't mind can u explain me which prperty will work to solve my problem..

Please Please Please eaplain me...

DateTime dt = dateTimePicker1.Value;
TimeSpan ts = DateTime.Now - dt;

Then use ts.TotalDays to do your calculation.

Please Please Please eaplain me...

No need for this.

Edited by jonsca: n/a


Besides the fact that a DateTimePicker is a lot more user friendly than a set of comboboxes, it has more to offer. Look here for more info and a code example.

Edited by ddanbe: n/a

Horses for courses..thats the right tool for the job :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.