在ASP.NET web.config全球标记中设置日期格式?

时间:2010-03-18 08:20:42

标签: asp.net date globalization

在我们的web.config中,我使用以下标记来确定ASP.NET网站的界面语言。

<globalization
   enableClientBasedCulture="true"        
   culture="auto:en-GB"
   uiCulture="auto:en"/>

这可以按预期工作:客户端请求特定的本地化获取它,其他人都很高兴地看着en-GB设置。

由于公司政策,我需要为每个人将日期格式更改为ISO 8601标准格式(YYYY-MM-DD)。这可能在web.config的中心位置,还是我需要在每个实例中手动更改?

添加:在将界面限制为英语时,是否可以获取此日期格式?

2 个答案:

答案 0 :(得分:6)

您应该使用CultureAndRegionInfoBuilder

build您自己的文化
 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();
            }
        }

答案 1 :(得分:2)

如果您需要跨文化格式相同的格式,则必须在实例化CultureInfo对象时设置DateTimeFormat。

此处没有全局配置选项。