代码之家  ›  专栏  ›  技术社区  ›  Jens

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

  •  5
  • Jens  · 技术社区  · 14 年前

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

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

    这是预期的工作:客户端wo请求一个特定的本地化得到它,其他人都很高兴看到en-gb设置。

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

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

    2 回复  |  直到 11 年前
        1
  •  6
  •   garik    14 年前

    你应该 build 使用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();
                }
            }
    
        2
  •  2
  •   Oded    14 年前

    如果需要跨文化使用相同的格式,则每当实例化 CultureInfo 对象。

    此没有全局配置选项。