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

发布于 2024-08-25 18:21:40 字数 397 浏览 3 评论 0原文

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

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

这按预期工作:客户请求特定的本地化,得到它,其他人都很高兴地查看 en-GB 设置。

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

补充:将界面限制为英文时是否可以获取此日期格式?

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"/>

This works as expected: Client wo request a specific localisation get it, everybody else is happily looking at the en-GB settings.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断爱 2024-09-01 18:21:40

您应该使用 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();
            }
        }

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();
            }
        }
埋情葬爱 2024-09-01 18:21:40

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

没有为此的全局配置选项。

If you need the format to be the same across cultures, you will have to set the DateTimeFormat whenever you instantiate a CultureInfo object.

There is no global config option for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文