1

i have 4 textbox ( Name , Password , Repeat Password and Date of birth)

Actually i have 2 methods to validate Name and Password

private static void ValidarNome(string Nome)
    {
        if (Nome.Trim().Length == 0)
        {
            string msg = " O nome não pode estar em branco";
            ApplicationException e = new ApplicationException(msg);
            throw e;
        }
    }
    private static void ValidarSenha(Int32 pw , Int32 pw2 , int check)
    {
        if (pw != pw2)
        {
            check = 1;
            string msg = " As senhas não conferem";
            ApplicationException e = new ApplicationException(msg);
            throw e;
        }
    }

But i need to pick the DateTime in Date of Birth Text and create a new method to validate if this DateTime is between 01/01/1960 - 31/12/1990 , what is the simple way to make this validation ?

EDIT - > With the help of users so was created the method to validate date >

private static void ValidarData(DateTime d,DateTime from,DateTime to, Int32 check2, Int32 check3)
    {
        if (d < from)
        {

            check2 = 1;
            string msg = " Data de nascimento não aceita";
            ApplicationException e = new ApplicationException(msg);
            throw e;
        }
        else if (d > to)
        {
            check3 = 1;
            string msg = " Data de nascimento não aceita";
            ApplicationException e = new ApplicationException(msg);
            throw e;
        }
        else
        {
            string msg = " Cadastro Incluído";
        }
    }

2 Answers2

3

Here you are:

DateTime from = new DateTime(1960,1,1);
DateTime to = new DateTime(1990, 12, 31);
DateTime input = DateTime.Now;
Console.WriteLine(from <= input && input <= to); // False
input = new DateTime(1960,1,1);
Console.WriteLine(from <= input && input <= to); // True

Hope this help.

hungndv
  • 2,121
  • 2
  • 19
  • 20
1

The DateTime structure overloads all of the standard comparison operators, so you can just compare them as you would numbers.

  • Note though that for equality the time down to fraction of a second must also be equal! – TaW Jul 05 '15 at 06:20