Compare two date attributes in the database with two Textbox

Asked

Viewed 405 times

4

How can I compare a range of two dates?

data_Inicio date
data_Fim date

textbox_inicio
textbox_fim

I can do this on the ASP.NET side and the goal is that my start date will never be less than my end date and my end date will never be less than my start date.

  • Put in your code so far

  • What is the Database?

1 answer

2


Consider using a date-specific component. Having said that, I will try to find a solution that seems to be just 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 put in the Github for future reference.

This is just a base, you can do better than this and you probably need to adapt to what you need since you didn’t post your code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.