Different DateTimeFormat for the same culture in different machines
Asked Answered
R

3

5

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 ShortDatePattern, using the same culture (pt-BR).

In my development machine (Windows 7, .NET 4 installed, application targeting .NET 3.5) and a Windows Server 2008 R2 (with the application targeting .NET 4) server the ShortDatePattern is "dd/MM/yyyy" - which is the correct, I guess.

In the production server (Windows Server 2003, using .NET 3.5) it is "d/M/yyyy". It is causing me tons of trouble.

I could solve the issue by setting the patterns by hand, but I'd really like to avoid doing this every time I need to output a date. Specially since this will be non-trivial in many places (like where I use MVC's Html.TextBoxFor) and will require a good amount of rewriting.

If there's a way of changing the patterns for the entire web application in one place it would be great. I've tried the following approach in the Global.asax.cs file, with no success:

CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
System.Threading.Thread.CurrentThread.CurrentCulture = info;

Any ideas?

Thank you.

Raffish answered 31/3, 2011 at 20:2 Comment(0)
R
4

Actually, I was trying to use the code above in the wrong place in the Global.asax file.

I managed to override the ShortDatePattern for the entire aplication by putting the code in the Application_BeginRequest method:

protected void Application_BeginRequest()
{
    CultureInfo info = new CultureInfo(System.Threading.Thread.CurrentThread.CurrentCulture.ToString());
    info.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
    System.Threading.Thread.CurrentThread.CurrentCulture = info;
}
Raffish answered 2/4, 2011 at 6:36 Comment(0)
K
3

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: /
Khorma answered 15/4, 2014 at 13:55 Comment(0)
J
0

in control panel go to regional and language options,select portuges(brazil), in formats go to customize this format, check the date configurationenter image description here.

Jury answered 31/3, 2011 at 20:16 Comment(1)
Though it would solve it on my machine, I can't change the formats on the server.Raffish

© 2022 - 2024 — McMap. All rights reserved.