3

Why does the following code generate a FormatException?

DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", null);

DateTime.ParseExact

Perhaps it has something to do with the fact that the code is running under IIS 7.5 Express as a part of an MVC3 site execution logic?

Maxim V. Pavlov
  • 10,303
  • 17
  • 74
  • 174
  • 2
    I ran it in LinqPad and had no problems. – Mario J Vargas Jan 24 '12 at 22:35
  • Try specifying the Invariant Culture if you're going to provide a defined conversion pattern, or providing a pattern more consistent with the culture of the running thread. – Tetsujin no Oni Jan 24 '12 at 22:36
  • I have added a proof-image of my case =) – Maxim V. Pavlov Jan 24 '12 at 22:40
  • I know it works with InvariantCulture. I was wondering why this particular case was throwing an exception. – Maxim V. Pavlov Jan 24 '12 at 22:41
  • InvariantCulture provides a default date format, which in this case matches the date string you have provided. What is the culture setting of the computer you're running this code on? Its date format apparently isn't the same as the string you provided in the statement. The null format provider value in your statement is making .NET use the computer's date format (culture info) as a default. – leanne Jan 24 '12 at 22:54
  • Thank you for explanations. The techical implication must be that slashes are culture sensetive, as Darjan mentioned. Other then that, I can see that I am providing enough info to the CLR object to know what to assign to Day, Month and Year properties on a resultant datetime object. – Maxim V. Pavlov Jan 24 '12 at 23:03

3 Answers3

5

You need to include CultureInfo, for example:

DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", new CultureInfo("en-US"));

Slashes in format string are culture sensitive and if you don't pass in CultureInfo, current culture is used. You can also use CultureInfo.InvariantCulture and it will work. Jon Skeet provides some detailed explanation here.

Community
  • 1
  • 1
doblak
  • 3,036
  • 1
  • 27
  • 22
  • The original method call depended on `CultureInfo.CurrentCulture.DateTimeFormat.DateSeparator` which might be `"/"`, `"-"`, `"."`, and so on. If you want to use a literal slash, and you don't want to use the `InvariantCulture`, escape the slash, as in `DateTime.ParseExact("03/01/2012", @"dd\/MM\/yyyy", null);`. – Jeppe Stig Nielsen Feb 12 '13 at 21:18
4

depends on your culture, to take that out of the equation....

DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture);
Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
3

According to the documentation, a FormatException is thrown, given one of these conditions:

public static DateTime ParseExact(
    string s,
    string format,
    IFormatProvider provider
) 
  • s or format is an empty string.
  • s does not contain a date and time that corresponds to the pattern specified in format.
  • The hour component and the AM/PM designator in s do not agree.

If you pass in a null IFormatProvider, I think it defaults to the current thread's culture. I'd have to look at this in Reflector. Is there any reason you wanted to pass in null?

UPDATE:

I looked at it in .NET Reflector and it defaults to the current thread's DateTimeFormatInfo. I don't know if I'm allowed to post the code here.

Mario J Vargas
  • 1,185
  • 6
  • 12