简体   繁体   English

DateTime.TryParseExact()返回混合结果

[英]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:43 2015-02-27 01:01:43
2015-02-27 01:01:53 2015-02-27 01:01:53
2015-02-27 01:02:05 2015-02-27 01:02:05
2015-02-27 01:02:15 2015-02-27 01:02:15
2015-02-27 01:02:36 2015-02-27 01:02:36
2015-02-27 01:02:51 2015-02-27 01:02:51
2015-02-27 01:03:04 2015-02-27 01:03:04
2015-02-27 01:03:21 2015-02-27 01:03:21
2015-02-27 01:03:36 2015-02-27 01:03:36
2015-02-27 01:03:46 2015-02-27 01:03:46
2015-02-27 01:04:01 2015-02-27 01:04:01
2015-02-27 01:04:13 2015-02-27 01:04:13
2015-02-27 01:04:29 2015-02-27 01:04:29
2015-02-27 01:04:40 2015-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM