Convert a dd / MM / yyy string to DateTime

1

I have a date filter that should receive the information from a textbox and convert it to DateTime, so that I can compare it to another date. The textbox sends the date in the format dd / MM / yyyy, but at the time of converting values where the date exceeds 12, an error occurs that says that the string can not be recognized as a DateTime.

I've looked at several sites and similar answers in this and in others, but nothing works out.

If there is not a simple way, I'll have to call the format string before going on the pro date and it's going to be the same.

Code

List<PainelFormularioMetodologia> PesquisaDataMaxima(List<PainelFormularioMetodologia> listaPesquisa)
    {
        List<PainelFormularioMetodologia> listaRetorno = new List<PainelFormularioMetodologia>();

        //O erro acontece aqui

        DateTime dataFim = DateTime.ParseExact(txtDataFim.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
        DateTime dataIni = DateTime.ParseExact(txtDataInicio.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);

       /////////////////////

        if (dataFim < dataIni)
            Util.Alert("Data final é menor que Data inicial.");

        foreach (PainelFormularioMetodologia item in listaPesquisa)
        {                
            if (item.Dt_ModificaProcesso_data <= dataFim)
                listaRetorno.Add(item);
        }

        if (listaRetorno.Count == 0)
            Util.Alert("Verifique o filtro: Modificado em");

        return listaRetorno;            
    }

Error

  

[FormatException: The DateTime represented by the string is not   supported in calendar System.Globalization.GregorianCalendar.]

Itworkedas:

DateTimedataFim=DateTime.ParseExact(txtDataFim.Text,"dd/MM/yyyy", CultureInfo.CreateSpecificCulture("pt-BR"));
DateTime dataIni = DateTime.ParseExact(txtDataInicio.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("pt-BR"));
    
asked by anonymous 08.04.2016 / 18:19

2 answers

3

Try to replace CultureInfo.InvariantCulture with CultureInfo.CreateSpecificCulture("pt-BR") - Richard Dias

It worked and looked like this:

DateTime dataFim = DateTime.ParseExact(txtDataFim.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("pt-BR"));
DateTime dataIni = DateTime.ParseExact(txtDataInicio.Text, "dd/MM/yyyy", CultureInfo.CreateSpecificCulture("pt-BR"));

Thanks to all who helped.

    
08.04.2016 / 19:19
-2

Try this:

DateTime data = DateTime.MinValue;

try {
 data = DateTime.Parse(dataString);
} catch{ data = DateTime.MinValue}

if(data.CompareTo(DateTime.MinValue) != 0)
  //data correta
else
  //data incorreta
    
08.04.2016 / 18:48