.NET DateTime Parse 抛出异常

.NET DateTime Parse throwing exception

我一直遇到一些对我来说真的很奇怪的问题,我无法开始理解为什么会这样。

基本上,我正在尝试做一个 DateTime.ParseExact 如果处理一种情况而不处理另一种情况。

假设我有这个字符串作为日期字符串:

"11/02/2015 11:59:06:313"

如果我通过给方法显式声明字符串来解析,即下一个代码,一切正常:

DateTime.ParseExact("11/02/2015 11:59:06:313", "dd/MM/yyyy HH:mm:ss:fff", null);

现在,当把它作为一个动态值(这是我想要的)时,我得到 "String not recognized as a valid DateTime format",在这段代码中:

DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:fff", null);

我也试过 Convert.ToDateTime 方法(抛出相同的异常):

Convert.ToDateTime(item.original).ToString("dd/MM/yyyy HH:mm:ss:fff");

'item.original' 变量来自 Class(它是 class 的字符串 属性,定义为

public class XmlData
{
    public string tableName { get; set; }
    public string columnName { get; set; }
    public string original { get; set; }
    public int required { get; set; }
    public string status { get; set; }
    public string type { get; set; }
    public string newValue { get; set; }
}

我真的迷路了。任何人都明白为什么会这样吗?

编辑

决定提供更多有关如何使用它的信息,因为问题可能来自更后面。

我有一个 class 用于仅定义使用反射将创建 DataTable 的属性。

class有这个属性:

public DateTime last_date { get; set; }

然后我使用这个方法来构建数据表:

public DataTable CreateEmptyDataTable(Type myType)
{
    DataTable dt = new DataTable();

    foreach (PropertyInfo info in myType.GetProperties())
    {
        dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
    }

    return dt;
}

初始化 DataTable 后,我从我的 XmlData class 中读取值并使用以下代码将值分配给 last_date 列,如下所示:

//Set the original state
foreach (XmlData item in collection)
{
    if (item.tableName == "vehicle")
    {
        if (item.original == "--NULL--")
            dr[item.columnName.Substring(item.tableName.Length + 1)] = DBNull.Value;
        else
        {
            if (item.type == "DT") //DateTime in format "dd/MM/yyyy HH:mm:ss:fff"
                dr[item.columnName.Substring(item.tableName.Length + 1)] = DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);
            else
                dr[item.columnName.Substring(item.tableName.Length + 1)] = Convert.ChangeType(item.original, dt.Columns[item.columnName.Substring(item.tableName.Length + 1)].DataType);
        }
    }
}

您的格式正确。

因为你使用 null 作为 IFormatProvider,我强烈怀疑你的 CurrentCulture has different DateSeparator than / or/and different TimeSeparator: 个字符。

/: 字符在自定义日期和时间解析中是特殊的。它们的意思是:用当前文化日期或时间分隔符替换我

在你的个人资料中,它说你来自葡萄牙语,你当前的文化可能是 pt-PT。而这种文化有 - 作为 DateSeparator

只是在黑暗中拍摄:

您有一个有效的硬编码值

DateTime.ParseExact("11/02/2015 11:59:06:313", "dd/MM/yyyy HH:mm:ss:fff", null);

但是当您将值分配给 last_date 列时:

if (item.type == "DT") //DateTime in format "dd/MM/yyyy HH:mm:ss:fff"
            dr[item.columnName.Substring(item.tableName.Length + 1)] = DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);
        else
            dr[item.columnName.Substring(item.tableName.Length + 1)] = Convert.ChangeType(item.original, dt.Columns[item.columnName.Substring(item.tableName.Length + 1)].DataType);

不应该是

DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:fff", null);

而不是

DateTime.ParseExact(item.original, "dd/MM/yyyy HH:mm:ss:FFF", null);

?

万一有人想知道,这个问题的 "solution" 是通过解构字符串并将其分配给 DateTime 构造函数来实例化 DateTime 对象,如下所示:

string[] date = item.original.Split(' ');
string[] datePart = date[0].Split('/');
string[] hourPart = date[1].Split(':');

DateTime newDateValue = DateTime.MinValue;

if (hourPart.Length == 3)
{
    newDateValue = new DateTime(Convert.ToInt32(datePart[2]), Convert.ToInt32(datePart[1]), Convert.ToInt32(datePart[0]), Convert.ToInt32(hourPart[0]), Convert.ToInt32(hourPart[1]), Convert.ToInt32(hourPart[2]));
}
if (hourPart.Length == 4)
{
    newDateValue = new DateTime(Convert.ToInt32(datePart[2]), Convert.ToInt32(datePart[1]), Convert.ToInt32(datePart[0]), Convert.ToInt32(hourPart[0]), Convert.ToInt32(hourPart[1]), Convert.ToInt32(hourPart[2]), Convert.ToInt32(hourPart[3]));
}

我用引号说 "solution" 是因为我认为这是一个糟糕的解决方案,并且可能有更好的解决方法,因为这个操作相对繁重但至少它有效