【问题标题】:Error with DateTime string Format [duplicate]DateTime 字符串格式错误 [重复]
【发布时间】:2018-12-25 13:43:21
【问题描述】:

我在转换字符串格式的日期时间时遇到问题,但我无法将其转换为日期时间格式。

我有错误:

字符串未被识别为有效的日期时间。有一个未知数 从索引 3 开始的单词

因为我阅读了一个带有日期字符串17-ago-18 的 .xmls 文档 我认为类似于"dd-MMM-yy"

有人可以帮我解决这个问题吗?

我的代码是:

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

【问题讨论】:

标签: c# ajax asp.net-mvc datetime asp.net-mvc-5


【解决方案1】:

解析器无法检测到您的字符串的语言。您必须明确告诉它日期是葡萄牙语。这就是Convert.ToDateTime 方法的第二个参数的用途。

方法文档:https://msdn.microsoft.com/en-us/library/9xk1h71t(v=vs.110).aspx

支持的文化代码的完整列表:https://msdn.microsoft.com/en-us/library/hh441729.aspx

使用示例:

CultureInfo culture = new CultureInfo("pt-BR");
DateTime dateTimeValue = Convert.ToDateTime(youDateString, culture);

【讨论】:

  • 非常感谢你的朋友,我一直在寻找答案,尝试使用英语和西班牙语,但我从未想过它会以葡萄牙语介绍。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多