简体   繁体   中英

DateTime.TryParseExact won't parse what appears to be a valid date string

This is going to be one of those "I can't believe I didn't think of that" questions, but it's been vexing me for going on two days.

I've got bunch of date strings (and many more columns of data) coming out of a .csv file that are in this format: 8/8/2017 8:57

I've read through the SO questions here and here as well as the documentation on MSDN from here and here .

Here are a couple of things I've tried in C# statements in LINQPad, adapted from the MSDN examples :

string[] data = { "8/8/2017 8:57,-1.220135,-1.239456,-3.20E-08,-4.47E-09,-1.202865"};
Console.WriteLine(data);

string dateValue = "8/8/2017 8:57";
string[] patterns = {"M/d/yyyy H:mm", "MM/dd/yyyy H:mm"};

DateTime parsedDate;

if (DateTime.TryParseExact(data[0].ToString(), patterns,
       System.Globalization.CultureInfo.CurrentCulture,
       System.Globalization.DateTimeStyles.None,
       out parsedDate))
{
    Console.WriteLine("Converted '{0}' to {1}.",
    dateValue,
    parsedDate);
}
else
{
    Console.WriteLine("Unable to convert '{0}' to a date and time.",
    dateValue);
}

if (DateTime.TryParseExact(data[0].ToString(),
    patterns,
    null,
    System.Globalization.DateTimeStyles.None,
    out parsedDate))
{
    Console.WriteLine("Converted '{0}' to {1}.",
    dateValue, 
    parsedDate);
}
else
{
    Console.WriteLine("Unable to convert '{0}' to a date and time.",
    dateValue);
}

Both result in:

Unable to convert '8/8/2017 8:57' to a date and time.

Unable to convert '8/8/2017 8:57' to a date and time.

I've tried several variants in the string[] patterns... declaration to no avail.

What am I missing? I suspect the problem lies in my patterns array, but maybe not? This seems like it should be easy.

I'm not married to DateTime.TryParseExact() , so long as I get a valid DateTime out of the conversion.

Edit: After some of the comments, I've checked my CultureInfo.CurrentCulture , and it is en-US , which should be fine, or at least I think so.

Edit1: As pointed out by most, I was using an entire string instead of the date value, although I still get an error using the single string. Here's the first if() modified as suggested by Joel below:

string[] data = "8/8/2017 8:57,-1.220135,-1.239456,-3.20E-08,-4.47E-09,".Split(',');
string dateValue = "8/8/2017 8:57";
string[] patterns = {"M/d/yyyy H:mm"};

DateTime parsedDate;

if (DateTime.TryParseExact(data[0], patterns,
       System.Globalization.CultureInfo.CurrentCulture,
       System.Globalization.DateTimeStyles.None,
       out parsedDate))
{
    Console.WriteLine("Converted '{0}' to {1}.",
    dateValue,
    parsedDate);
}
else
{
    Console.WriteLine("Unable to convert '{0}' to a date and time.",
    dateValue);
}

I've managed to incorporate a version of this into my production code that's working as expected. Thanks for the help.

Look carefully at your data:

string[] data = { "8/8/2017 8:57,-1.220135,-1.239456,-3.20E-08,-4.47E-09,-1.202865"};

This array has only one element, where the value of that element is all of the text. It seems different in the console messages because you put the 8/8/2017 8:57 value into a separate variable, rather than using the array.

Did you perhaps want this instead?

string[] data={"8/8/2017 8:57","-1.22013","-1.239456","-3.20E-08","-4.47E-09","-1.202865"};

Or maybe you wanted this:

var data = "8/8/2017 8:57,-1.220135,-1.239456,-3.20E-08,-4.47E-09,-1.202865".Split(',');

**Note I don't generally condone using .Split() as a csv parser, but for this simple example it gets the point across.

Whatever you do, I'd make sure your Console messages accurately reflect what you tried to do (use data[0] as the first substitution), so you can be sure the TryParseExact() method is looking at the string you think it is.

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