在ASP.NET web.config全球化标签中设置日期格式? [英] Setting a date format in ASP.NET web.config globalization tag?
问题描述
在我们的web.config中,我使用以下标签确定ASP.NET网站的界面语言。
In our web.config I am using the following tag to determine the interface language of an ASP.NET website.
<globalization
enableClientBasedCulture="true"
culture="auto:en-GB"
uiCulture="auto:en"/>
这样可以预期:客户端请求一个特定的本地化获取它,其他人都高兴地看着en-GB设置。
This works as expected: Client wo request a specific localisation get it, everybody else is happily looking at the en-GB settings.
由于公司政策,我需要将日期格式更改为每个人的ISO 8601标准格式(YYYY-MM-DD)。这是否可能在web.config的中心位置,或者我需要在每个实例中手动更改?
Due to company policy I need to change the date format to the ISO 8601 standard format (YYYY-MM-DD) for everybody. Is this possible at a central place in the web.config or do I need to change this manually in every instance?
添加:是否会限制界面到英文时可以得到这个日期格式?
Addition: Would it be possible to do get this date format when restricting the interface to english?
推荐答案
你应该构建您自己的文化,使用CultureAndRegionInfoBuilder
You should build your own culture by using CultureAndRegionInfoBuilder
class Program
{
static void Main(string[] args)
{
CultureInfo ci;
CultureAndRegionInfoBuilder cib = null;
try
{
// Create a CultureAndRegionInfoBuilder object named "x-en-GB".
Console.WriteLine("Create and explore the CultureAndRegionInfoBuilder...\n");
cib = new CultureAndRegionInfoBuilder(
"x-en-GB", CultureAndRegionModifiers.None);
// Populate the new CultureAndRegionInfoBuilder object with culture information.
ci = new CultureInfo("en-GB");
ci.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
//ci.DateTimeFormat.FullDateTimePattern = "yyyy-MM-dd";
//ci.DateTimeFormat.LongDatePattern = "yyyy-MM-dd";
//...
//...
cib.LoadDataFromCultureInfo(ci);
// Populate the new CultureAndRegionInfoBuilder object with region information.
RegionInfo ri = new RegionInfo("GB");
cib.LoadDataFromRegionInfo(ri);
Console.WriteLine("Register the custom culture...");
cib.Register();
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("Create and explore the custom culture...\n");
ci = new CultureInfo("x-en-GB");
//Thread.CurrentThread.CurrentCulture = ci;
//Thread.CurrentThread.CurrentUICulture = ci;
Console.WriteLine(DateTime.Now.ToString(ci));
Console.ReadLine();
}
}
这篇关于在ASP.NET web.config全球化标签中设置日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!