Click here to Skip to main content
15,900,405 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
C#
string input1 = "12/12/1991"

DateTime dt;

res = DateTime.TryParseExact(input1, "MM/dd/yyyy", null,System.Globalization.DateTimeStyles.None, out dt);

     if (res == true)
     {
             int year = DateTime.Now.Year - dt.Year;
             int month = DateTime.Now.Month - dt.Month;
             if (month < 0)
             {
               year = year - 1;
               month = month + 12;
             }
             if (year > 18)
             {
               Console.WriteLine("1");
             }
             else
             {
               Console.WriteLine("-1");
             }
      }

I am not getting any output for the above code... Help appreciated

[edit]Code block added - OriginalGriff[/edit]
[edit]Formatted code block- Rohan Leuva[/edit]
Posted
Updated 16-Dec-13 8:06am
v4
Comments
Richard MacCutchan 16-Dec-13 13:59pm    
Step through the code in the debugger and you will see exactly what is happening.
Sergey Alexandrovich Kryukov 16-Dec-13 14:12pm    
"if (res == true)" tells the tale. Can you see why? :-)
—SA
[no name] 16-Dec-13 14:18pm    
"if (res == true)...." remembers me always also to:
if (x > y)
res= true;
else
res= false;
Arjun Abco 16-Dec-13 15:11pm    
there is a semicolon for the string input and the res s declared as a bool... i didnt type it here like that.. bu still its not jus entering the loop and i hav no idea y so!!

Interesting - I do.
I get "1".

However, have a look at this: Working with Age: it's not the same as a TimeSpan![^] - I knocked it up when I needed an age-related class a while ago, and it should be a little clearer to use than your code:
C#
string input1 = "12/12/1991";
DateTime dt;
if (DateTime.TryParseExact(input1, "MM/dd/yyyy", null, System.Globalization.DateTimeStyles.None, out dt))
    {
    Age age = new Age(dt);
    if (age.Years >= 18)
        {
        Console.WriteLine("1");
        }
    else
        {
        Console.WriteLine("-1");
        }
    }
 
Share this answer
 
What are you trying to accomplish? What is your intended goal for this code? I had to add a ';' to the end of input 1 and define res as a bool.


Using the below code, I received a console output of '1'.

C#
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input1 = "12/12/1991";

            DateTime dt;

            bool res = DateTime.TryParseExact(input1, "MM/dd/yyyy", null, System.Globalization.DateTimeStyles.None, out dt);

            if (res == true)
            {
                int year = DateTime.Now.Year - dt.Year;
                int month = DateTime.Now.Month - dt.Month;
                if (month < 0)
                {
                    year = year - 1;
                    month = month + 12;
                }
                if (year > 18)
                {
                    Console.WriteLine("1");
                }
                else
                {
                    Console.WriteLine("-1");
                }
            }
        }
    }
}
 
Share this answer
 
Comments
Arjun Abco 16-Dec-13 15:13pm    
i hav the semicolon for the string and hav also declared the res as bool. i didnt type it here.. bu i am not getting ny output.. its jus a blank console screen

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900