I don't understand Why there is an overload with IFormatProvider in DateTime.ParseExact
?
If I'm defining exactly how it should be parsed ( spaces , separators etc) , then theres should be no problem :
All these 3 examples show the same result:
example 1
CultureInfo provider =CultureInfo.CreateSpecificCulture("en-US");
var t= DateTime.ParseExact("13-2-2013", "d-M-yyyy", provider, DateTimeStyles.None);
Console.WriteLine (t); //13/02/2013 00:00:00
example 2
CultureInfo provider =CultureInfo.CreateSpecificCulture("en-US");
var t= DateTime.ParseExact("13/2/2013", "d/M/yyyy", provider, DateTimeStyles.None);
Console.WriteLine (t); //13/02/2013 00:00:00
example 3
CultureInfo provider =CultureInfo.CreateSpecificCulture("en-US");
var t= DateTime.ParseExact("13@@@2@@@2013", "d@@@M@@@yyyy", provider, DateTimeStyles.None);
Console.WriteLine (t); //13/02/2013 00:00:00
So why do i need to provide provider If I'm explicitly defining the structure ?
There are still format specifiers that are culture dependant, like the time separator (:) and the date separator (/). Those doesn't match a specific character, but the separator specified in the culture.
Because:
:
and /
in the format string do not represent literal characters but rather the separator as specified by the format provider (see the bottom of the table here).If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With