C# 如何使用linq将字符串datetime解析为real datetime?

C# 如何使用linq将字符串datetime解析为real datetime?,c#,.net,linq,datetime,C#,.net,Linq,Datetime,我的到期日是“2015年10月31日12:00:00 AM”,意思是MM/dd/YYYY hh:MM:ss AM,但它是SAP的字符串。如何将其转换为MM/dd/YYYY 以下代码不工作。错误: “字符串未被识别为有效的日期时间。” 我如何使用linq实现这一点 var query = deliveriesItemsbypaging.Select(tb => tb.ExpiryDate) .AsEnumerable() // Do the rest of the

我的到期日是“2015年10月31日12:00:00 AM”,意思是MM/dd/YYYY hh:MM:ss AM,但它是SAP的字符串。如何将其转换为MM/dd/YYYY 以下代码不工作。错误:

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

我如何使用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);
你可以这样试试

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));

您可以在此处使用
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"));

使用
DateTime.ParseExact()
时,需要使用特定的字符串格式
“M/d/yyyy h:mm:ss tt”格式,下一次使用
.ToString(“mm/dd/yyyy”)

List dateTimes=new List();
dateTimes.Add(“2015年10月31日上午12:00:00”);
var selectValue=dateTimes.Select(d=>d)
.可计算的()
.Select(d=>DateTime.ParseExact(d,“M/d/yyyy h:mm:ss tt”,CultureInfo.InvariantCulture).ToString(“mm/dd/yyyy”)).ToList();
var r=DateTime.ParseExact(“2015年10月31日12:00:00 AM”,“M/d/yyyy h:mm:ss tt”,CultureInfo.InvariantCulture);
结果:


下面的方法可以将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;
    }

为什么不使用正确的格式?例如:
DateTime.ParseExact(“10/31/2015 12:00:00 AM”,“M/d/yyyy h:mm:ss tt”,CultureInfo.InvariantCulture)我觉得你的代码还可以。检查
ExpiryDate
中的值,可能您的值不正确
ExpiryDate
的数据类型是什么?为什么它在数据库本身中不是一个
DateTime
?将日期存储为字符串是一个非常严重的错误。与其尝试解析数据,不如修复数据库模式一般问题是:“7/31/2014 12:00:00 AM”发生时“7/31/2014 12:00:00 AM”不会被解析为“MM/dd/yyyy”。这个月需要2位数字。OP的代码也应该有效,因为他传递的是一个空的
char[]
,它使用所有空白字符作为分隔符:“分隔符的每个元素定义一个单独的分隔符,该分隔符由单个字符组成。如果分隔符参数为null或不包含任何字符,则该方法将空白字符视为分隔符。”。因此,您的方法是OP的子集。这将重新运行一个日期时间对象,然后仍然需要将其转换为string.use DateTime.Parse(x,CultureInfo.InvariantCulture).Date.ToString(“MM/dd/yyyy”)或更简单的DateTime.Parse(x,CultureInfo.InvariantCulture).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);
    string DateConverter(string date)
    {
        string[] dmy= date.Split(' ')[0].Split('/');
        string convertedDay = dmy[1] + "/" + dmy[0] + "/" + dmy[2];
        return convertedDay;
    }