【问题标题】:Error in converting string to datetime in C#.net在 C#.net 中将字符串转换为日期时间时出错
【发布时间】:2012-06-26 11:06:13
【问题描述】:

在 C# 中将字符串转换为日期时出错。 错误是:
string is not valid datetime.
下面是我用来将字符串转换为日期时间的代码。

string[] DateFormat = { "dd-MM-yyyy", "dd/MM/yyyy", "MM-dd-yyyy", "MM/dd/yyyy" ,"dd-MMM-yy"};
            VendorSinceDate = DateTime.ParseExact(dtresult.Rows[0]["VendorSinceDate"].ToString(),DateFormat,System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.None);

//dtresult.Rows[0]["VendorSinceDate"].ToString()="06-Jun-12 12:00:00 AM";

【问题讨论】:

  • 除了邀请 Bobble Tables 之外,您可能犯的最大错误莫过于将日期存储在文本类型的表格列中。修好桌子。

标签: c#-4.0 datetime date-format


【解决方案1】:

您可能的 DateFormat 都不包含时间,但您尝试转换的字符串包含时间。

您需要添加一个匹配的 DateFormat,例如dd-MMM-yy hh:mm:ss tt

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 2013-12-22
    • 1970-01-01
    相关资源
    最近更新 更多