4 Replies - 376 Views - Last Post: 19 September 2013 - 02:36 PM Rate Topic: -----

#1 bkruep  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 26-July 13

Birthdate

Posted 19 September 2013 - 02:18 PM

I am hoping this is a very striaght forward question. A portion of the program I am writing wants me to ask user for the age in month, date, and year and then I will subtract that date from the current date. This is what I have but my issue and I hope I explain this is setting the month, date, and year together in one variable if that makes sense....I know there is a problem with this line but I am not sure how to fix it

DateTime DOB = new DateTime(birthYear, birthMonth, birthDate);




Here is the entire method



        public int FindAge()
        {
            int age;
            Console.Write("Enter your birthday month: ", birthMonth);
            birthMonth = Convert.ToDateTime(Console.ReadLine());

            Console.Write("Enter your birthday date: ", birthDate);
            birthDate = Convert.ToDateTime(Console.ReadLine());

            Console.Write("Enter your year of birth: ", birthYear);
            birthYear = Convert.ToDateTime(Console.ReadLine());

            DateTime DOB = new DateTime(birthYear, birthMonth, birthDate);

            DateTime currentDate = DateTime.Now;

            //age = DOB - currentDate;

            Console.WriteLine();
            //Console.WriteLine("Your age is {0}", age);

            return age;
        }




Is This A Good Question/Topic? 0
  • +

Replies To: Birthdate

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,015
  • Joined: 12-June 08

Re: Birthdate

Posted 19 September 2013 - 02:24 PM

What's the error, and have you looked at the Datetime object constructors?

http://msdn.microsof...e.datetime.aspx

You are trying to make a date time out of shoving three more date times into it? I am not sure if I see that constructor or even approve of that logic. I mean.. if the day, month, and year were, say, integers that might make more sense.. but each a datetime object? It seems odd.
Was This Post Helpful? 0
  • +
  • -

#3 bkruep  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 26-July 13

Re: Birthdate

Posted 19 September 2013 - 02:30 PM

Ok that might be my problem. If it was just integers and asked me to find my age in years I would just subtract the current year from the birth year but in this program I need to use month and date which has me confused on what to do
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3331
  • View blog
  • Posts: 11,281
  • Joined: 12-December 12

Re: Birthdate

Posted 19 September 2013 - 02:36 PM

Read the link that modi123_1 provided which details how to properly use the DateTime constructor.

You essentially need to collect three integer values (for the year, month and day) then use these three numbers to construct a DateTime value (their date of birth). The you can subtract one date form the other.

dotnetperls Subtract example

This post has been edited by andrewsw: 19 September 2013 - 02:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4454
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Birthdate

Posted 19 September 2013 - 02:36 PM

Quote

Ok that might be my problem


No "might" about it, Modi is correct. Instead of treating each input as a DateTime, you should treat each as an Int32 (which is the same as int). Then you pass those integers the same way you were trying to do with the three DateTimes you made.

A DateTime represents a full date value, not part of one. You can't just give DateTime a month and expect it to know what to do with it.

However, you should also check the input:

private int GetIntFromConsole()
{
    while (true)
    {
        string input = Console.ReadLine();
        int result;
        if (int.TryParse(input, out result))
        {
            return result;
        }
        Console.Write("Could not understand input. Please try again: ");
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1