Different DateTimeFormat for the same culture in different machines

后端 未结 3 926
面向向阳花
面向向阳花 2021-01-19 06:32

I\'m having a problem when I deploy my web application in different servers. There seems to be an inconsistency in some DateTimeFormat patterns, like Shor

3条回答
  •  被撕碎了的回忆
    2021-01-19 06:47

    The solution is to always create CultureInfo object using the constructor:

    CultureInfo(string name, bool useUserOverride)
    

    and passing false for useUserOverride parameter.

    From MSDN:

    useUserOverride: A Boolean that denotes whether to use the user-selected culture settings (true) or the default culture settings (false).

    Basically using false force the CultureInfo to use the default settings (separators, ...) from the specified culture instead of using the one defined in the system.

    Consider also that different operating system can produce different results in some (small) cases. Running the code below (.NET 4.5):

    CultureInfo ci = new CultureInfo("it-IT", false);
    
    String date = DateTime.Now.ToString(ci);
    Console.WriteLine(date);
    Console.WriteLine("Time Separator: " + ci.DateTimeFormat.TimeSeparator);
    Console.WriteLine("Date Separaotr: " + ci.DateTimeFormat.DateSeparator);
    Console.ReadKey();
    

    On Win 7 produce:

    29/10/2013 14:12:33
    Time Separator: :
    Date Separaotr: /
    

    while running it on Win 8 produce:

    29/10/2013 15.08.43
    Time Separator: .
    Date Separaotr: /
    

提交回复
热议问题