ASP date format using client culture code-behind

时光怂恿深爱的人放手 提交于 2019-12-08 07:37:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!