0

Im having a problem converting a datetime which is in string format but I am not able to convert it to DateTime format.

i have the error:

the string was not recognized as a valid datetime. there is an unknown word starting at index 3

because i read an .xmls document with date string 17-ago-18 i think is something like "dd-MMM-yy"

someone can help me to solve this?

my code is:

    public JsonResult Pedidos(List<string[]> lines)
    {
        using (TransactionScope scope = HelperTransactionScope.getTransactionScope())
        {

            try
            {
                for (int i = 1; i < lines.Count; i++)
                {
                    if (lines[i].Length == 1)
                        return Json("400");

                    PedidosFundilag pedido = new PedidosFundilag();
                    pedido.Cliente = lines[i][0];
                    pedido.Division = lines[i][1];
                    pedido.NumParte = lines[i][2];
                    pedido.Cantidad = Convert.ToInt32(lines[i][3]);
                    pedido.FechaSolicitud = Convert.ToDateTime(lines[i][4]);         
                    pedido.FechaEnvio = Convert.ToDateTime(lines[i][5]);
                    pedido.OrdenCompra = lines[i][6];
                    pedido.NumConfirmacion = lines[i][7];

                    AgregarPedido(pedido);
                }

                scope.Complete();
                return Json("200");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("", ex.InnerException == null ? ex.Message : ex.InnerException.InnerException != null ? ex.InnerException.InnerException.Message : ex.InnerException.Message);
                return Json(ex.Message, "400");
            }
Nathalia Soragge
  • 1,415
  • 6
  • 21

1 Answers1

0

The parser is not able do detect the language of your string. You'll have to explicitly tell it that the date is in Portuguese. This is what the second argument of the Convert.ToDateTime method is for.

Method documentation: https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx

Full list of culture codes supported: https://msdn.microsoft.com/en-us/library/hh441729.aspx

Example of use:

CultureInfo culture = new CultureInfo("pt-BR");
DateTime dateTimeValue = Convert.ToDateTime(youDateString, culture);
Nathalia Soragge
  • 1,415
  • 6
  • 21
  • thank you very much friend, I was looking for a lot the answer, try in English and spanish but I never would have imagined that it is being introduced in Portuguese. thank you very much. – Programador S Jul 17 '18 at 21:26