Check if a date is valid

2

I have a college job where I need to check if a date is valid but I can not use the DateTime and TimeSpan classes. With these classes I can even do it, but without being able to use them at the teacher's discretion, I can not do it. The image shows how the project should be, the rest I have already done, just need to verify if the date that is entered is valid or not! Can anyone give me any tips on how to try to resolve this?

    
asked by anonymous 29.09.2017 / 00:06

3 answers

1

I think you can split the MaskedTextBox string by giving a split, using the '/' character, convert the numbers to integer, and then validate.

  

In MaskedTextBox , property TextMaskFormat must be IncludeLiterals which is the default setting.

Here's an example:

public static void Main()
{
    string data = "31/03/2018"; //Aqui você coloca: seuMaskedTextBox.Text;

    int maiorAnoPermitido = 2050;
    int menorAnoPermitido = 2000;

    string[] val = data.Split('/');

    int dia, mes, ano;

    if (int.TryParse(val[0] , out dia) && int.TryParse(val[1], out mes) && int.TryParse(val[2], out ano))
    {
                if (ano >= menorAnoPermitido && ano <= maiorAnoPermitido)
                {
                    if (mes >=1 && mes <=12)
                    {

                        int maxDia = (mes==2 ? ( ano % 4 ==0 ? 29 : 28  ) : mes <=7 ? (mes%2==0?30 : 31) : (mes%2==0?31:30));                       

                        if (dia >=1 && dia <=maxDia)
                        {
                            Console.WriteLine("Data Válida");
                        }
                        else
                        {
                            Console.WriteLine("Dia inválido");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Mês inválido");
                    }
                }
                else
                {
                    Console.WriteLine("Ano inválido");
                }
    }
    else
    {
        Console.WriteLine("Data inválida");
    }

}

Now, with more time, I made the validation even of the days next to the month, besides defining variables for the greater and smaller year that the system must accept.

I put it in .NETFiddle: link

    
29.09.2017 / 00:37
0

Although the object returned by Convert.ToDateTime is of type DateTime , it was not used directly from class DateTime ... I think it could be like this:

bool validaData;

try {
    Convert.ToDateTime(txtData.Text);
    validaData = true;
} catch (Exception exx) {
    validaData = false;
}

if(validaData){
    //Válida!
} else {
    //Não é válida!
}
    
29.09.2017 / 00:38
0

You can get the text portions of the field and transform them into numbers:

string textoBruto = this.TextBoxN.Text;
int dia = Convert.ToInt32(textoBruto.Substring(0, 2), 10); // onde 10 significa base decimal
int mes = Convert.ToInt32(textoBruto.Substring(2, 2), 10);
int ano = Convert.ToInt32(textoBruto.Substring(4, 4), 10);

I suppose that the bars are not part of the text ... I do not remember how this component behaves, but if there are bars, just take with a Replace .

And if these conversions fail, it's because they were not valid integers anyway.

Now just do two simple checks: if the month is between 1 and 12, and if the days are between 1 and the maximum of the month.

if (mes < 1 || mes > 12) {
    throw new DataInvalidaMeuParsaException();
}

A hint for getting the biggest possible day of the month: you can create a hashmap, or cheat with an Array:

int[] maioresDias = new int[] {
    31,
    ano % 4 == 0 ? 29 : 28, // verificação de bissexto
    31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};

And now:

if (dia < 1 || dia > maioresDias[mes - 1]) {
    throw new ParaCeTaLokException();
}

Ex .: merely illustrative exceptions. .NET has some more appropriate exception types that already come with it, such as ArgumentException or ArgumentOutOfRangeException .

    
29.09.2017 / 00:39