Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change en-US dates to en-GB for asp.net?

on a developer machine (cassini)

new DateTime(2012,3,14).ToString("d")

results in

14/03/2012

which is correct but when deployed to a full IIS server the result is

03/14/2012

The server is set in control panel/Region language to all English/UK/GB, running date in command prompt returns the dd/MM/YYYY format.

The site is set for both uiCulture="en-GB" and culture="en-GB" and these show in the web.config globalization tag.

I can work around this issue by adding a forced culture

new DateTime(2012,3,14).ToString("d", new CultureInfo("en-GB"));

but I would really like to know what is setting the format incorrectly.

CultureInfo.CurrentCulture.Name, CultureInfo.CurrentUICulture.Name

both return en-US


  • en-US: M/d/yyyy (e.g. 3/14/2012)
  • en-GB: dd/MM/yyyy (e.g. 14/03/2012)

Actual value in web.config

 <globalization requestEncoding="UTF-8" responseEncoding="UTF-8" uiCulture="en-GB" culture="en-GB" />
like image 228
rob Avatar asked Mar 14 '12 11:03

rob


People also ask

How to change Date format of Date in c#?

If you already have it as a DateTime , use: string x = dt. ToString("yyyy-MM-dd");


2 Answers

I managed to get it working by putting this into the web.config

<globalization culture="en-GB"/>
like image 161
Jon Avatar answered Oct 21 '22 02:10

Jon


In your web.config add

<globalization culture='auto' uiCulture='auto' />

and then, assuming the browser is correctly configured to pass the preferred locale, the worker thread processing the request will have its CurrentCulture and CurrentUICulture set correctly.

Any locale dependent operations (including such things as DateTime format d) will use the client's preference.

Globalization element of web.config on MSDN: https://msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71).aspx

like image 31
Richard Avatar answered Oct 21 '22 01:10

Richard