如何使用LINQ将字符串日期时间解析为实际日期时间?

4
我的过期日期是“10/31/2015 12:00:00 AM”,表示为MM/dd/YYYY hh:mm:ss AM,但它是来自SAP的字符串。如何将其转换为MM/dd/YYYY?下面的代码不起作用,出现错误:

"String was not recognized as a valid DateTime."

我该如何使用linq实现这个功能?

var query = deliveriesItemsbypaging.Select(tb => tb.ExpiryDate)
              .AsEnumerable() // Do the rest of the processing locally
              .Select(x => DateTime.ParseExact(x.Split(new char[0])[0], "MM/dd/yyyy", CultureInfo.InvariantCulture));

这篇博客的代码是有效的:
 var r =   DateTime.ParseExact("10/31/2015 12:00:00 AM".Split(new char[0])[0], "MM/dd/yyyy", CultureInfo.InvariantCulture);

为什么你不使用正确的格式?例如:DateTime.ParseExact("10/31/2015 12:00:00 AM", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture); - M. Wiśnicki
你的代码看起来没问题。检查一下你的“ExpiryDate”中的值,可能有错误的值。 - Pikoh
2
ExpiryDate 的数据类型是什么?为什么数据库本身不是 DateTime 类型?将日期存储为字符串是一个非常严重的错误。与其尝试解析数据,不如修复数据库架构。 - Panagiotis Kanavos
通常问题是:“2014年7月31日上午12:00:00”发生的时候。 - Penguen
“7/31/2014 12:00:00 AM”不能使用“MM/dd/yyyy”进行解析。它需要月份的2个数字。 - Pikoh
显示剩余2条评论
5个回答

4

您可以在此处使用 DateTime.Parse

var query = deliveriesItemsbypaging.Select(tb => tb.ExpiryDate)
              .AsEnumerable() // Do the rest of the processing locally
              .Select(x => DateTime.Parse(x, CultureInfo.InvariantCulture).Date);

.Date 在此处只会提供日期,不包括时间,根据您的需要。

更新: 如果您想要获取字符串的可枚举对象(采用特定格式),您可能需要将其重写为:

var query = deliveriesItemsbypaging.Select(tb => tb.ExpiryDate)
              .AsEnumerable() // Do the rest of the processing locally
              .Select(x => DateTime.Parse(x, CultureInfo.InvariantCulture).ToString("MM/dd/YYYY"));

这将返回一个日期时间对象,仍需要转换为字符串。 - Ghasan غسان
1
使用DateTime.Parse(x, CultureInfo.InvariantCulture).Date.ToString("MM/dd/yyyy")或者更简单的方式,DateTime.Parse(x, CultureInfo.InvariantCulture).ToString("MM/dd/yyyy")。 - Heorhiy Pavlovych

3

在使用DateTime.ParseExact()方法时,需要使用特定的字符串格式,具体为"M/d/yyyy h:mm:ss tt",然后使用.ToString("MM/dd/yyyy")方法。

List<string> dateTimes = new List<string>();
dateTimes.Add("10/31/2015 12:00:00 AM");
var selectValue = dateTimes.Select(d => d)
       .AsEnumerable()
       .Select(d => DateTime.ParseExact(d, "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy")).ToList();

var r = DateTime.ParseExact("10/31/2015 12:00:00 AM", "M/d/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

结果:

在此输入图片描述


1
你可以尝试像这样做。
var query = deliveriesItemsbypaging.Select(tb => tb.ExpiryDate)
              .AsEnumerable() // Do the rest of the processing locally
              .Select(x => DateTime.ParseExact(x.Split(new char[]{' '})[0], "MM/dd/yyyy", CultureInfo.InvariantCulture));

2
OP 的代码也应该可以工作,因为他传递了一个空的 char[],它使用所有的空白字符作为分隔符。MSDN:_"separator 中的每个元素都定义了一个单个字符组成的分隔符。如果 separator 参数为 null 或包含零个字符,则此方法将把空白字符视为分隔符。"_ 因此,你的方法是 OP 方法的子集。 - Tim Schmelter

1
var query = deliveriesItemsbypaging
    .Select(tb => tb.ExpiryDate)
    .AsEnumerable() // Do the rest of the processing locally
    .Select(x => DateTime.ParseExact(x, "MM/dd/yyyy hh:mm:ss tt", CultureInfo.InvariantCulture).ToString("MM/dd/yyyy"));

1
以下方法可以将 MM/dd/YYYY hh:mm:ss AM 格式的日期转换为可转换的字符串日期。
    string DateConverter(string date)
    {
        string[] dmy= date.Split(' ')[0].Split('/');
        string convertedDay = dmy[1] + "/" + dmy[0] + "/" + dmy[2];
        return convertedDay;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接