简体   繁体   中英

DateTime.TryParseExact() is returning mixed results

I'm trying to parse a huge file that contains some lines that are DateTime. For some reason DateTime.TryParseExact is only returning true on some of of the lines and not others.
My DateParse.txt file looks something like this:

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

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. 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. That's why after parsing operation their DateTime values will be;

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 .

改变这一行并检查

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

Just Change the mm mont to MM

DateTime.TryParseExact(line, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeUniversal, out DateTime)

It 'll work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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