Setting a date format in ASP.NET web.config globalization tag?

前端 未结 2 875
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 18:15

In our web.config I am using the following tag to determine the interface language of an ASP.NET website.



        
相关标签:
2条回答
  • 2020-12-10 18:48

    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();
                }
            }
    
    0 讨论(0)
  • 2020-12-10 19:04

    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.

    0 讨论(0)
提交回复
热议问题