【问题标题】:Getting error while parsing string to datetime将字符串解析为日期时间时出错
【发布时间】:2017-01-04 14:59:21
【问题描述】:

我在 CSV 文件中有一个日期时间值,下面是示例数据:

20/06/2016 11:52
21/06/2016 11:52
22/06/2016 11:52

当我尝试解析这个日期时间时,我得到了错误:

字符串未被识别为有效的日期时间。

我不确定这个日期的格式是什么,但我总是希望在我的应用程序将使用的文化中解析它。因此,根据当前的文化,我想解析我的日期。

这就是我尝试的方式,但出现上述错误:

string row = "20/06/2016 11:52" 

试一试

CultureInfo culture = CultureInfo.CurrentCulture;
  DateTimeStyles styles = DateTimeStyles.None;
  DateTime dateValue;
  DateTime.TryParse(rowValue, culture, styles, out dateValue); // {1/1/0001 12:00:00 AM}

试试 2

 DateTimeFormatInfo usDtfi = new CultureInfo(culture.Name, false).DateTimeFormat;
  var l = Convert.ToDateTime(rowValue, usDtfi); //String was not recognized as a valid DateTime
  var g = DateTime.Parse(rowValue, usDtfi);//String was not recognized as a valid DateTime

以上所有这些方法都失败了,我想知道确切的日期并希望将其存储在我的 SQL Server 数据库表中。

我的系统日期时间格式为:mm/dd/yy

我已经看到了一些类似下面的问题:

String was not recognized as a valid DateTime " format dd/MM/yyyy"

Datetime format Issue: String was not recognized as a valid DateTime

但所有这些答案都指定了日期格式,但我不知道格式是什么;这就是为什么我试图从当前的文化中发现。我不确定我的想法是否正确。

【问题讨论】:

  • 可能是因为没有秒数。使用 TryParseExact
  • 您解析的日期是否以各种格式存储在文件中?所以你可以在文件中有多个日期,每个日期都有不同的格式?
  • 看看下面这个方法,它支持多种日期格式进行解析。 DateTime.ParseExact 方法(String, String[], IFormatProvider, DateTimeStyles)msdn.microsoft.com/en-us/library/332de853(v=vs.110).aspx
  • @Rabid penguin:是的,日期可以有多种格式,所以我希望根据我当前的文化存储日期时间

标签: c# .net datetime


【解决方案1】:

如果您确定遇到的每个字符串都采用正确的格式,但您只是不知道是哪一个,您可以做的一件事是在您的机器上获取所有不同格式的数组并使用它在解析精确方法中:

var formats = (from CultureInfo ct in CultureInfo.GetCultures(CultureTypes.AllCultures)
               select ct.DateTimeFormat.GetAllDateTimePatterns()).SelectMany((x) => x).ToArray();
DateTime test = DateTime.ParseExact("20/06/2016 11:52", formats, CultureInfo.InvariantCulture, DateTimeStyles.AssumeLocal);

我机器上的代码生成了超过 26,000 种格式。只要字符串跟随其中之一,就会被接受。

如果得到正确的格式会不一致,你可以走这条路:

var formats = (from CultureInfo ct in CultureInfo.GetCultures(CultureTypes.AllCultures)
               select ct.DateTimeFormat);
string dateString = "20/06/2016 11:52";
DateTime temp = new DateTime(0);
foreach (DateTimeFormatInfo dfi in formats)
{
    if (DateTime.TryParseExact(dateString, dfi.GetAllDateTimePatterns(), dfi, DateTimeStyles.None, out temp))
    {
        break;
    }
}
if(temp == new DateTime(0))
{
    //save string to get it's format
}

【讨论】:

  • 其实我不知道,因为日期范是 dd/mm/yy 格式,也可以是 mm/dd/yy 格式。用户可以以任何格式将日期时间存储在 fike 中
  • 我的意思是它是否总是正确的格式,或者说它可以像这样2006/2016 11:52 有错误吗?
  • 不,它将始终采用如下格式:6/20/2006 或 20/6/2016
  • 然后,我首先提出的代码应该可以工作,因为正如我所指出的,它会生成超过 26,000 种格式来检查字符串。
  • 好的,感谢您为帮助我所做的努力。将检查并通知您。其实我不确定我的想法是否正确
【解决方案2】:

如果你 100% 确定格式总是相同的,你可以使用 ParseExact 方法,比如:

var parsedDate = DateTime.ParseExact(row, "dd/MM/yyyy hh:mm", CultureInfo.InvariantCulture);

【讨论】:

  • 查看我更新的问题,因为有时这不是格式
  • 您知道它将在哪种格式之间变化吗?还是可以是各种格式?
  • 查看我的日期时间可以是 mm/dd/yy,也可以是 dd/mm/yy,也可以是 yy/dd/mm 小时分钟和秒。所以我想把它存储在我的sql表中
猜你喜欢
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-28
  • 2019-09-14
  • 1970-01-01
相关资源
最近更新 更多