views:

275

answers:

4

Hi, I'm having some difficulties parsing strings of DateTime using DateTime.ParseExact.

    DateTime result;
    CultureInfo provider = CultureInfo.InvariantCulture;

    // Parse date-only value with invariant culture.
    //format = "mm/dd/yyyy";
format = "d";
 try
    {

        result = DateTime.ParseExact(data+" 12:00:00 AM", format, provider);
    }

data is a string variable loaded with dates of the format "5/20/2009". I tried tacking on a fake time just to see if that would work, and it didn't(with or without the tack on). I also tried using the "g" format specifier and it did not work, I always get the exception that it isn't a valid DateTime string. The only dates it works for is like "12/20/2009" (notice that 2 digits in the "MM" part)

I can not get this routine to work with single digit months! Microsofts own example from MSDN is

6/15/2009 1:45:30 PM -> 6/15/2009 (en-US)

and it will not work with this. I am just not understanding what I am doing wrong. I have also tried my own format specifiers like "mm/dd/yyyy" and "MM/dd/yyyy" but to no avail.

+4  A: 

If you want to parse a DateTime in en-US format, you have to specify the en-US culture:

DateTime.ParseExact("6/15/2009", "d", CultureInfo.GetCultureInfo("en-US"));
dtb
Why must I specify that culture? also, that is not ParseExact. We know exactly what format the date strings are coming from, why can a format string not be used? Using this, we could bring in things that shouldn't be there(specifying time in a datefield is a no no)
Earlz
I've changed my example to ParseExact. You must specify a culture because you say you want your input to be parsed in a culture specific way (namely in en-US format).
dtb
Ok, I just thought Invariant meant US.. thanks so much!
Earlz
+4  A: 

I think you want to use:

format = "M/dd/yyyy";

Notice only 1 M.

See this article on MSDN for confirmation and further details.

Dan Tao
No, thats wrong. That will make it so that "11/15/2009" turns to "1/15/2009"
Earlz
Have you actually tried it? Because I have, and it seems to work (11/15/2009 does not become 1/15/2009). The 1 'M' simply indicates not to add a zero in front of one-digit months (much like calling ToString("0") on an Integer returns the number without any padding zeroes).
Dan Tao
Dan's right. You don't understand what the format string means if you think it'll convert 11 -> 1 without the extra M.
davewasthere
hmm.. that actually does work, maybe I used "m" instead of "M" when I tested it(or maybe that was before your edit)
Earlz
+1  A: 

ParseExact requires that you use the exact long format specifier (e.g. mM/dd/yyyy) instead of just "d"

Jeff Leonard
That will still give an exception. If I tack on a "0" to make somethin like "06/20/2009" then it will parse everything correct, but the month will be 1, so it'll be "1/20/2009" and this is making no sense.
Earlz
A: 

The character '/' when used in format strings is just a placeholder for the culture-specific date separation character. If you want it to be literal you must escape it with '\'.

If I understood your problem correctly you may be able to parse your text with:

DateTime.ParseExact(data, "MM\/dd\/yyyy", CultureInfo.InvariantProvider);
Jorge Alves