Validating date and time

4

As I do so that in the date field, not saved a date that has passed, for example the one of yesterday and in the hour field do not enter an invalid time, for example 27:00, only the Brazilian hours. Both fields are MaskedTextBox .

Date Validation:

public static bool ValidaData(string maskdata)
        {
            DateTime resultado = DateTime.MinValue; 
            if (DateTime.TryParse("dd/MM/yyyy", out resultado))    
            return true; 
            return false;   

Date verification:

if (clnValidacoes.ValidaData(maskdata.Text) == false)
            {
                MessageBox.Show("Data Inválida!");
                maskdata.Focus();
            }

But every date I put gives "Invalid." On time validation:

public static bool ValidaHora(string maskhora)
        {
            String hora = "";
            String[] hms = hora.split(":");
            int horas = Integer.parseInt(hms[0]);
            int segundos = Integer.parseInt(hms[2]);
            int minutos = Integer.parseInt(hms[1]);
            if (horas > 24)
            {
                return false;
            }
            else
            {
                return true;                
            }
        }
    }
}

You get an error in Interger.

    
asked by anonymous 03.07.2016 / 02:08

2 answers

2

The code posted in the question does not make sense. In fact it does not compile, it has parts that is even C #. The question also does not make it very clear what the expected result is, but I did what I could to help.

First I removed redundancies. It would be interesting to learn how languages really work, all existing operators, etc. This makes it easier to do the right thing and simplify codes.

If you want to get a date that you know is always right (comes from a date-stamped control), you do not need to use TryParse() . To get a date in a specific format, it's best to use ParseExact() . I have my doubts if you need this or if this is the proper format, but I reproduce what's in the question.

To catch the day of yesterday you have to catch the day of today minus one. I have my doubts if that is what you need. This is a naive implementation of how to do this. You may need to look at something else.

If the time is coming from your own control, the time should be ok, but if not, maybe Split() will no longer work. It would have to do a parser . As I'm not sure what the intention was to reproduce what was in the code but using C #. I have questions if this check is appropriate.

public static void Main() {
    if (!ValidaData("24/10/2016")) { WriteLine("invalido"); }
    if (!ValidaHora("27:10:15")) { WriteLine("invalido"); }
}
public static bool ValidaData(string maskdata) {
    return DateTime.ParseExact(maskdata, "dd/MM/yyyy", CultureInfo.InvariantCulture) <= DateTime.Now.AddDays(-1);
}

public static bool ValidaHora(string maskhora) {
    return Int32.Parse(maskhora.Split(':')[0]) <= 24;
}

See running on dotNetFiddle and on CodingGround .

    
03.07.2016 / 18:11
0

Dude, I understand that you have two problems:

  • You need to make sure it's a date
  • This date must be greater than the current date
  • It is best to work with the date and time together and check if they join make up a valid date, and then check if the date is less than the current date:

            public static bool ValidaDataHora(string data, string hora)
            {
                DateTimeFormatInfo brDtfi = new CultureInfo("pt-BR", false).DateTimeFormat;
                try
                {
                    DateTime dataehora = Convert.ToDateTime(string.Format("{0} {1}", data, hora), brDtfi);
                    if(dataehora < DateTime.Now)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                catch
                {
                    return false;
                }
            }
    
        
    04.07.2016 / 22:42