1

When I write this code date set to something like that "12/12/2018 20:08" and it's ok

{
     var newCulture = new CultureInfo("en-GB");
     var date = (value as DateTime?)?.ToString(newCulture.DateTimeFormat.ShortDatePattern+" HH:mm", CultureInfo.InvariantCulture);
}

but when i change culture name to "en-Us" the result is "12.12.2018 20:08" (not right format as i know) -returning date format of ShortDatePattern is not right

I test it on another desktop, and there it works right with 2 of them. May be there is some kind of dependence on desktop settings? Why date formats are various?

1 Answers1

3

new CultureInfo("en-GB") calls the ctor with useUserOverride set to true. This means that if you server environment has a match for the specified culture with custom settings those will be used instead of the default once. So to fix your issue CultureInfo("en-GB", false); or CultureInfo.GetCultureInfo("en-GB") (for a cached version)

The GetCultureInfo method retrieves a cached, read-only CultureInfo object. It offers better performance than a corresponding call to the CultureInfo.CultureInfo(String) constructor.

If name is the name of the current culture, the returned CultureInfo object does not reflect any user overrides. This makes the method suitable for server applications or tools that do not have a real user account on the system and that need to load multiple cultures efficiently.

Rotem
  • 21,452
  • 6
  • 62
  • 109
Magnus
  • 45,362
  • 8
  • 80
  • 118