Compare two date attributes in the database with two TextBox

4

How can I compare a range of two dates?

data_Inicio date
data_Fim date

textbox_inicio
textbox_fim

I'll be able to do this soon on the ASP.NET side, and the goal is that my start date is never less than my end date and my end date is never less than my start date.

>     
asked by anonymous 24.11.2014 / 14:54

1 answer

2

Consider using a component for dates . That being said I'll try to give a solution to what seems to be what you need:

DateTime dataInicio;
if (!DateTime.TryParse(textbox_inicio.Text, out dataInicio)) {
    lblErro.Text =  "Formato da data inicial é inválido";
    lblErro.Visible = true;
    return;
}
DateTime dataFim;
if (!DateTime.TryParse(textbox_fim.Text, out dataFim)) {
    lblErro.Text =  "Formato da data final é inválido";
    lblErro.Visible = true;
    return;
}

if (DateTime.Compare(dataInicio.Date > dataFinal.Date) {
    lblErro.Text = "Data inicial não pode ser superior à data final";
    lblErro.Visible = true;
} else {
    lblErro.Text = "";
    lblErro.Visible = false;
}

I placed GitHub for future reference .

This is just a base, you can do better than this and you will probably need to adapt to what you need since you have not posted your code.

    
24.11.2014 / 17:11