hi,

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;
            if 
       (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))

                --years;

            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 6 Years Ago by virusisfound: n/a

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

Int32.Parse()  

or

Int32.TryParse()  

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 3 Years Ago 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
                years--;
            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 6 Years Ago 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 6 Years Ago by ddanbe: n/a

Comments
Horses for courses..thats the right tool for the job :)
This article has been dead for over six months. Start a new discussion instead.