Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CultureInfo on DateTime.ParseExact?

Tags:

c#

.net

datetime

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 ?

like image 456
Royi Namir Avatar asked Feb 13 '13 10:02

Royi Namir


2 Answers

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.

like image 78
Guffa Avatar answered Sep 24 '22 00:09

Guffa


Because:

  1. The specified format can include localized names of weekdays and months.
  2. The characters : 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).
like image 31
Jon Avatar answered Sep 20 '22 00:09

Jon