Doubt about date c #

0

my question is how can it be done to control that a person is greater than 25 when that person Enter your date of birth.

 public DateTime Fecha
    {
        get { return fecha; }
        set { fecha = value; }

    }
    
asked by Francop 17.07.2017 в 22:29
source

2 answers

1

You could try this:

public DateTime Fecha
        {
            get { return fecha; }
            set { 
                  int edad = DateTime.Today.AddTicks(-value.Ticks).Year - 1;
                   if edad>25{
                           fecha = value;
                    }
           }

        }
    
answered by 17.07.2017 в 22:37
1

You can use another property like this:

public DateTime Fecha { get; set; }

public bool IsMayorDeEdad
{
    get
    {
        var today = DateTime.Today;
        // Calculate the age.
        var age = today.Year - Fecha.Year;
        // Go back to the year the person was born in case of a leap year
        if (Fecha > today.AddYears(-age)) {age--;}
        return age > 25;
    }
}
    
answered by 17.07.2017 в 22:50