4

Why should we use IFormatProvider in DateTime.ParseExact if there is already a format parameter?

DateTime.ParseExact(inputString, format, cultureInfo);
MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Dim_Ka
  • 788
  • 1
  • 7
  • 26

2 Answers2

8

The format parameter says what pattern to use - but it doesn't say anything about which calendar, month names, short date format etc to use. That's up to the IFormatProvider.

For example, suppose you wanted to parse a value with the pattern "dd MMMM yyyy" - which month names would you expect to work? If you're using a month name of "February" but you're running on a machine with a system culture of French, it would fail - you'd need to specify an English culture (or the invariant culture) to get it to work. Likewise, you could specify a pattern of "d" to mean the short date format - but which short date format?

Even the calendar you use is affected by the format provider: the value could be parsed into the same year, month and day values in two cultures - but the meaning of those values would be very different in a Hijri calendar from a Gregorian calendar, for example.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Ahmedilyas: It helps that I'm rather interested in dates and times :) – Jon Skeet Nov 29 '13 at 13:55
  • yes. I know the feeling sometimes.... don't get me started with clients and their requirements and clientside calculation and different regions and.... urgh! :) – Ahmed ilyas Nov 29 '13 at 13:56
1

A simple example: /

/ is not just a char, but a date separator that depends on the culture.

ken2k
  • 48,145
  • 10
  • 116
  • 176