为什么Convert.ChangeType()不将日期字符串(dd/MM/yyyy格式)转换为日期时间类型

本文关键字:日期 格式 yyyy 类型 时间 MM 转换 字符串 ChangeType Convert 为什么 | 更新日期: 2023-09-27 18:04:19

下面是我打算通过反射填充实体数据的代码。

public static void SetEntityProperty(object entity, DevExpress.Web.ASPxFormLayout formLayoutControl)
{
    if (formLayoutControl != null)
    {
       Type type = entity.GetType();
       System.Reflection.PropertyInfo[] properties = type.GetProperties();
       System.Data.DataTable dt = new System.Data.DataTable();
       foreach (System.Reflection.PropertyInfo pi in properties)
       {
          var layoutItem = formLayoutControl.FindItemByFieldName(pi.Name);
          if (layoutItem != null && layoutItem.CssClass == "ro-item") continue;
          var control = formLayoutControl.FindNestedControlByFieldName(pi.Name);
          if (control != null && control is DevExpress.Web.ASPxEdit)
          {
              var targetType = Data.IsNullableType(pi.PropertyType) ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;
              try
              {
                 if ((control as DevExpress.Web.ASPxEdit).Value != null)
                 {
                    //here (control as DevExpress.Web.ASPxEdit).Value = "19/05/2015" and control is a read only text box
                    var value = Convert.ChangeType((control as DevExpress.Web.ASPxEdit).Value, targetType);
                    if (value != null && value is System.String)
                    {
                        value = value.ToString().Trim();
                        (control as DevExpress.Web.ASPxEdit).Value = value;
                    }
                    pi.SetValue(entity, value, null);
                 }
                 else
                 {
                    pi.SetValue(entity, null, null);
                 }
              }
              catch (Exception ex)
              {
                 pi.SetValue(entity, value, null);
              }
         }
         else
              throw ex;
       }
    }
}

我在这里做错了什么?我发现了一些与Convert.ChangeType相关的其他问题,但所有这些建议都已在该代码中实现。我使用的是Visual Studio 2013 C#,DevExpress 14.2.6。请帮忙。谢谢

为什么Convert.ChangeType()不将日期字符串(dd/MM/yyyy格式)转换为日期时间类型

试试这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
namespace DateTimeConvert
{
    class Program
    {
        static void Main(string[] args)
        {
            var text1 = "19/05/2015"; //dd/mm/yyyy format
            var text2 = "05/19/2015"; //mm/dd/yyyy format
            var result = DateTime.Now;
            DateTime.TryParse(text1, out result);
            Console.WriteLine(result);
            DateTime.TryParse(text2, out result);
            Console.WriteLine(result);
            //Depending on computer's current culture format this will cause an error due to dd/MM/yyyy CULTURE format
            //DateTime date = (DateTime)Convert.ChangeType(text1, typeof(DateTime));
            var date = DateTime.ParseExact(text1, "dd/MM/yyyy", CultureInfo.InvariantCulture);
            Console.WriteLine(date);
            Console.ReadLine();
        }
    }
}

实际情况是,您正在转换"dd/MM/yyyy"的日期-时间格式。这取决于你电脑的文化格式。您可以使用DateTime.ParseExact来指示使用CultureInfo.InvariantCulture的正确格式。

不要养成吞下异常的习惯,甚至不要处理你不知道的异常。

在这种情况下,您的应用程序很可能无法解析提供的日期。

不要使用Convert类来分析文本字符串。请改用Parse方法。

var resultDate = DateTime.ParseExact(((Control)DevExpress.Web.ASPxEdit).Value, "dd/MM/yyyy", System.Globalization.CultureInfo.CurrentUICulture)