我如何使用 linq 将字符串日期时间解析为实际日期时间?
How can i use linq for parsing string datetime to real datetime?
我的 ExpiryDate 是“10/31/2015 12:00:00 AM”,意思是 MM/dd/YYYY hh:mm:ss AM 但它是来自 SAP 的字符串。我如何转换它 MM/dd/YYYY
以下代码不是 working.error :
"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);
你可以这样试试
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"));
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"));
在使用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);
结果:
以下方法可以将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;
}
我的 ExpiryDate 是“10/31/2015 12:00:00 AM”,意思是 MM/dd/YYYY hh:mm:ss AM 但它是来自 SAP 的字符串。我如何转换它 MM/dd/YYYY 以下代码不是 working.error :
"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);
你可以这样试试
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"));
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"));
在使用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);
结果:
以下方法可以将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;
}