[英]DateTime.TryParseExact() is returning mixed results
I'm trying to parse a huge file that contains some lines that are DateTime. 我正在尝试解析一个包含一些DateTime线的巨大文件。 For some reason DateTime.TryParseExact is only returning true on some of of the lines and not others.
由于某种原因,DateTime.TryParseExact仅在某些行上返回true而不在其他行上返回true。
My DateParse.txt file looks something like this: 我的DateParse.txt文件看起来像这样:
2015-02-27 01:01:30
2015-02-27 01:01:30
2015-02-27 01:01:432015-02-27 01:01:43
2015-02-27 01:01:532015-02-27 01:01:53
2015-02-27 01:02:052015-02-27 01:02:05
2015-02-27 01:02:152015-02-27 01:02:15
2015-02-27 01:02:362015-02-27 01:02:36
2015-02-27 01:02:512015-02-27 01:02:51
2015-02-27 01:03:042015-02-27 01:03:04
2015-02-27 01:03:212015-02-27 01:03:21
2015-02-27 01:03:362015-02-27 01:03:36
2015-02-27 01:03:462015-02-27 01:03:46
2015-02-27 01:04:012015-02-27 01:04:01
2015-02-27 01:04:132015-02-27 01:04:13
2015-02-27 01:04:292015-02-27 01:04:29
2015-02-27 01:04:402015-02-27 01:04:40
string line;
DateTime DateTime;
using (StreamReader Date = new StreamReader("C:\\DateParse.txt")
while((line = Date.ReadLine()) != null)
if (DateTime.TryParseExact(line, "yyyy-mm-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out DateTime))
Console.WriteLine("True");
else
Console.WriteLine("False");
My output is: 我的输出是:
False
假
False假
False假
False假
True真正
True真正
True真正
True真正
And the rest are false...其余的都是假的......
Any help in figuring this out would be greatly appreciated. 任何帮助解决这个问题将不胜感激。
Because MM
specifier for months and mm
specifier for minutes. 因为
MM
说明符为月份, mm
说明符为分钟。
In your case, only if your string have the same minute and month value it will be parsed. 在您的情况下, 只有当您的字符串具有相同的分钟和月份值时,才会对其进行解析。
That's why only these values parsed successfully. 这就是为什么只有这些值成功解析的原因。
2015-02-27 01:02:05
2015-02-27 01:02:15
2015-02-27 01:02:36
2015-02-27 01:02:51
By the way, when I mean successfully, that doesn't mean it returns the right DateTime
value. 顺便说一下,当我的意思是成功时,这并不意味着它返回正确的
DateTime
值。 Since you didn't mentioned any month part with yyyy-mm-dd HH:mm:ss
format, your DateTime
's month part will 1
by default. 由于您没有提到
yyyy-mm-dd HH:mm:ss
格式的任何月份部分,因此您的DateTime
的月份部分默认为1
。 That's why after parsing operation their DateTime
values will be; 这就是为什么在解析操作后他们的
DateTime
值会是;
2015-01-27 01:02:05
2015-01-27 01:02:15
2015-01-27 01:02:36
2015-01-27 01:02:51
which is wrong values based on your strings. 这是基于您的字符串的错误值。
In your case, right format should be yyyy-MM-dd HH:mm:ss
. 在你的情况下,正确的格式应该是
yyyy-MM-dd HH:mm:ss
。
改变这一行并检查
if (DateTime.TryParseExact(line, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out DateTime))
using (StreamReader Date = new StreamReader("C:\\DateParse.txt")
while((line = Date.ReadLine()) != null)
if (DateTime.TryParseExact(line.Trim(), "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out DateTime))
Console.WriteLine("True");
else
Console.WriteLine("False");
use line.Trim()
and yyyy-MM-dd HH:mm:ss
where MM
for month representation and mm
for minutes 使用
line.Trim()
和yyyy-MM-dd HH:mm:ss
其中MM
表示月份, mm
表示分钟
Just Change the mm mont to MM 只需将mm mont更改为MM即可
DateTime.TryParseExact(line, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out DateTime)
It 'll work 它会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.