[英]parse string to custom date format c#
I have trouble parsing a string to a correct DateTime.我无法将字符串解析为正确的 DateTime。 I am aware of DateTime.ParseExact but I keep raising exception.
我知道 DateTime.ParseExact 但我一直在引发异常。 What did I miss here ?
我在这里错过了什么?
const string formatString = "yyyyMMdd HHmmss";
const string fileName = @"C:\path.csv";
IEnumerable<Foo> dtos = File.ReadAllLines(fileName)
.Skip(1)
.Select(line => new {line, columns = line.Split(';')})
.Select(t =>
{
try
{
return new Foo
{
Bar = DateTime.ParseExact(t.columns[0], formatString, CultureInfo.InvariantCulture, DateTimeStyles.None)
};
}
catch (FormatException)
{
Console.WriteLine("{0} is not in the correct format.", t.columns[0])
}
return null;
});
catch message is捕获消息是
20110102 170100 is not in the correct format.
20110102 170100 格式不正确。
EDIT : Link to a screenshot of the error as asked by helpers (SO don't let new accounts to add images).编辑:链接到帮助者询问的错误屏幕截图(所以不要让新帐户添加图像)。 enter image description here
在此处输入图片说明
The code works.该代码有效。 You must be reading different data :
您必须阅读不同的数据:
const string formatString = "yyyyMMdd HHmmss";
static void Main(string[] args)
{
string input = "20110102 170100";
DateTime Bar = DateTime.ParseExact(input, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.