问题
I have an ASP.NET web application and data in a SQLServer DB.
What is the best way to display a date (extracted with LINQ) in the client's culture format, from code-behind.
I mean I have users from USA and from Europe, they want different formats: MM/dd/yyyy (US) or dd/MM/yyyy (UK)?
What I would like is something like:
from myData in dbContext.myFile
Where .../...
Select myFile.birthDate.ToString.(**some magic formating here**)
Update: Thanks to Darin for quick answer!
Tip: if using IE, don't forget to check what is the preferred lang in use: Look in Tools / Internet options / Languages
回答1:
You could set the culture to auto in the <globalization> element of your web.config:
<globalization culture="auto" uiCulture="auto" />
which will use the culture of the client browser. Then simply use .ToString():
<%= DateTime.Now.ToString() %>
or ToShortDateString depending on the format you want.
来源:https://stackoverflow.com/questions/7777694/asp-date-format-using-client-culture-code-behind