-1

I've been trying to create a Days on Earth Calculator in C#, where the user can input their birthdate in MM/DD/YYYY format and find out how long they've been on this earth(in days). I already checked out a similar question that someone posted 2 years ago titled "How many days user has been alive calculator". But my issue lies within a format exception. Here is what I tried to do (I'm very new at this):

Console.WriteLine("Welcome to the Days on Earth Finder!" +
"\nPlease input your birthday(MM/DD/YYY):");
Console.ReadLine();

//string myBirthday = Console.ReadLine();
//DateTime mB = Convert.ToDateTime(myBirthday);
//DateTime myBirthday = DateTime.Parse(Console.ReadLine());
string myBirthday = Console.ReadLine();
DateTime mB = DateTime.Parse(myBirthday); //This line is where the error occurs
TimeSpan myAge = DateTime.Now.Subtract(mB);

Console.WriteLine("You are " + myAge.TotalDays + " days old!");
Console.ReadLine();

I left my previous attempts commented out if it helps. The error that comes up is as follows:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

Additional information: String was not recognized as a valid DateTime.

Although when I give it a string literal it works, for example "8/10/1995".

DateTime myBirthday = DateTime.Parse("8/10/1995");
TimeSpan myAge = DateTime.Now.Subtract(myBirthday);
Console.WriteLine("You are " + myAge.TotalDays + " days old!");
Console.ReadLine();

Also I'm using Visual Studio 2015 Community RC if that helps.

Community
  • 1
  • 1
Ninjatix
  • 75
  • 1
  • 1
  • 7
  • The input you are entering is not in the correct format. – aleksandar Jul 04 '15 at 17:54
  • I agree with the above. First Try hard coding a date to see if the rest of the code runs. Next, enclose the parse function in a try catch. Anything relating to user input has to be error checked, because there is no guarantee the user will give the proper input. Check out this site for the specific documentation on the function breaking for you: https://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx – Bwvolleyball Jul 04 '15 at 17:57
  • What are you using as input? When i run your program with input from console it runs without errors. – aleksandar Jul 04 '15 at 18:03
  • I use 8/10/1995 as the input when it pops up in the console. – Ninjatix Jul 04 '15 at 18:05
  • 1
    And then you press enter again and it gives the exception. That's because you coded it to read the input two times and the second time is when you use the input as the date but you are pressing enter (which is not a date) and it gives `FormatException`. – aleksandar Jul 04 '15 at 18:09
  • I changed it, the two instances of `Console.ReadLine();` was the issue. – Ninjatix Jul 04 '15 at 18:12

3 Answers3

1

You can try with something like this:

string birthDateString = "5/2/1992";
DateTime birthDate;
if (DateTime.TryParse(birthDateString, out birthDate))
{
    DateTime today = DateTime.Now;
    Console.WriteLine("You are {0} days old", (today - birthDate).Days);
}
else Console.WriteLine("Incorrect date format!");
msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • Yeah, if I set the string to something like "5/2/1992" it works but when I ask the user to input their own birthdate in MM/DD/YYYY format it doesn't. Why doesn't the latter work but the former does? – Ninjatix Jul 04 '15 at 18:10
1

You have two Console.ReadLine(); statements.

You probably have to press enter twice.

Remove the first Console.ReadLine(); and it will work when you fill in the date.

Wtower
  • 18,848
  • 11
  • 103
  • 80
  • Pretty sure this is why it's not working, although @msmolcic answer is probably a better way to do it than the original code – Sam Jul 04 '15 at 18:04
  • Probably, but I thought the question was why it was not working and is not a complete solution. ;) – Farhad Kaveh Jul 04 '15 at 18:10
1

You coded it to read the input two times and the second time is when you use the input as the date but then you are probably pressing enter (which is not a date) and it gives FormatException.

You should remove the first ReadLine().

Console.WriteLine("Welcome to the Days on Earth Finder!" +
"\nPlease input your birthday(MM/DD/YYY):");
//Console.ReadLine();

//string myBirthday = Console.ReadLine();
//DateTime mB = Convert.ToDateTime(myBirthday);
//DateTime myBirthday = DateTime.Parse(Console.ReadLine());
string myBirthday = Console.ReadLine();
DateTime mB = DateTime.Parse(myBirthday);
//This line is where the error occurs
TimeSpan myAge = DateTime.Now.Subtract(mB);

Console.WriteLine("You are " + myAge.TotalDays + " days old!");
Console.ReadLine();
aleksandar
  • 2,399
  • 2
  • 14
  • 22