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"));