Dan Scott
Dan Scott

Reputation: 41

And & Or operators in if statements

I've made a date of birth program that display your DoB, it will also factor in leap years so that if you enter year as 1995, month 2, day 29, it will through an error because it wasn't a leap year. It also takes into a how many days each month has and this is that part I want help with. Currently, this is how it is:

           if (month == 1 / && day > 31)
            {
                Console.WriteLine("January only has 31 days - Enter again");
                validDay = false;
            }
            else if (day >= 29 && !IsLeapYear(year) && month == 2)
            {
                Console.WriteLine("You were not born on a leap year or February only has 28 days - Enter again");
                validDay = false;
            }
           //And so on for each month

So I then tried it like this:

if (month == 1 || month == 3 || month == 5 || month == 7 || 
                month == 8 || month == 10 || month == 12 && day > 31)
            {
                Console.WriteLine("Your birth month only has 31 days - Enter again");
                validDay = false;
            }
            else if (day >= 29 && !IsLeapYear(year) && month == 2)
            {
                Console.WriteLine("You were not born on a leap year or February only has 28 days - Enter again");
                validDay = false;
            }
            else if (month == 3 || month == 6 || month == 9 || month == 11 && day > 30)
            {
                Console.WriteLine("Your birth month only has 30 days - Enter again");
                validDay = false;
            }

And for some reason it will always throw the error message even if you enter 1997, 5, 28. The month chosen in 5 but the day is not > 31 so why does it do this? I'm also open up to different ways to do this because I have a feeling there's another way - arrays maybe?

Upvotes: 2

Views: 196

Answers (5)

gpmurthy
gpmurthy

Reputation: 2427

I would recommend using DateTime.TryParseExact as follows...

string year = "1997";
string month = "5";
string day = "28";
string dateText = string.Format("{0}/{1}/{3}", year, month, day);
DateTime date;

if (!DateTime.TryParseExact(dateText, "yyyy/MM/dd", null, DateTimeStyles.None, out date))
{
    Console.WriteLine("Date is invalid");
}

Good Luck!

Upvotes: 0

user3014562
user3014562

Reputation:

Try adding some parenthesis

if ((month == 1 || month == 3 || month == 5 || month == 7 || 
month == 8 || month == 10 || month == 12) && day > 31)

Without parethesis the last && refers only to the last month.

You can also use the DateTime constructor and see if it throws an exception (http://msdn.microsoft.com/en-us/library/xcfzdy4x(v=vs.110).aspx) or use DateTime.TryParse.

Upvotes: 9

Why not use DateTime.TryParse.

Please have a look at the following links as well:

Validating Date (Leap year or not) before inserting into Database

Validate a DateTime in C#

Upvotes: 0

CinCout
CinCout

Reputation: 9619

Operators have something called precedence order. Try using parentheses in order to achieve the precedence you wish to achieve.

Upvotes: 0

dougczar
dougczar

Reputation: 595

I would recommend you use DateTime.TryParse - it will validate all dates automatically, then you could just return something like "Invalid date" to the user.

Upvotes: 1

Related Questions