Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# asp.net内核中基于区域设置的时间格式_C#_Asp.net Core_Asp.net Core 2.0_Asp.net Core Localization - Fatal编程技术网

C# asp.net内核中基于区域设置的时间格式

C# asp.net内核中基于区域设置的时间格式,c#,asp.net-core,asp.net-core-2.0,asp.net-core-localization,C#,Asp.net Core,Asp.net Core 2.0,Asp.net Core Localization,是否可以根据区域设置更改时间格式 让我们看看下面的场景 挪威-24小时时间格式被广泛使用 瑞典-24小时时间格式被广泛使用 瑞士-德国地区-广泛使用12小时格式 德国-广泛使用24小时制 因此,我的最终问题是,如何在asp.net核心c中基于区域设置时间 我已经做了本地化的日期,但我必须这样做的时间以及 上图显示的是上午/下午。同样,我需要以24小时格式显示时段,该格式根据区域设置而定。好的,我希望这是您想要的 首先,您需要支持实际的区域性,并在应用程序启动时配置它们 public void C

是否可以根据区域设置更改时间格式

让我们看看下面的场景

挪威-24小时时间格式被广泛使用 瑞典-24小时时间格式被广泛使用 瑞士-德国地区-广泛使用12小时格式 德国-广泛使用24小时制 因此,我的最终问题是,如何在asp.net核心c中基于区域设置时间

我已经做了本地化的日期,但我必须这样做的时间以及


上图显示的是上午/下午。同样,我需要以24小时格式显示时段,该格式根据区域设置而定。

好的,我希望这是您想要的

首先,您需要支持实际的区域性,并在应用程序启动时配置它们

public void ConfigureServices(IServiceCollection services)
{
    /*boilerplate code omitted*/

    // Configure supported cultures and localization options
    services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("de-DE"),
            new CultureInfo("fr"),
            new CultureInfo("ar-YE")
        };

        // State what the default culture for your application is. This will be used if no specific culture
        // can be determined for a given request.
        options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");

        // You must explicitly state which cultures your application supports.
        // These are the cultures the app supports for formatting numbers, dates, etc.
        options.SupportedCultures = supportedCultures;

        // These are the cultures the app supports for UI strings, i.e. we have localized resources for.
        options.SupportedUICultures = supportedCultures;
    });
}
然后您需要实际使用请求本地化

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(locOptions.Value);
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}
现在,无论何时将日期对象从应用程序推送到客户机,它都将在当前客户机区域设置中解析它

如果您正在使用Google Chrome并想测试它,您只需访问chrome://settings/languages 更改浏览器的区域设置并更改设置。重新启动Chrome,您将立即看到更改


参考资料:

如果您已经很好地完成了本地化,则无需执行其他步骤以本地格式显示时间

但以防万一;您可以在配置本地化时定义特定区域性的时间格式,方法是提供您自己的格式,如@Marco回复中修改的代码所示:

public void ConfigureServices(IServiceCollection services)
{
/*boilerplate code omitted*/

// Configure supported cultures and localization options
services.Configure<RequestLocalizationOptions>(options =>
{
    var supportedCultures = new[]
    {
        new CultureInfo("en-US"),
        new CultureInfo("de-DE"),
        new CultureInfo("fr"),
        new CultureInfo("ar-YE") {                    
                DateTimeFormat = {
                    // change long time pattern to 24 hours 
                    // e.g. 13:50:21
                    LongTimePattern = "HH:mm:ss",

                    // short time pattern as 12 hours
                    // e.g. 01:50:21 PM
                    ShortTimePattern = "hh:mm tt"
                },
            }
    };

    // State what the default culture for your application is. This will be used if no specific culture
    // can be determined for a given request.
    options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");

    // You must explicitly state which cultures your application supports.
    // These are the cultures the app supports for formatting numbers, dates, etc.
    options.SupportedCultures = supportedCultures;

    // These are the cultures the app supports for UI strings, i.e. we have localized resources for.
    options.SupportedUICultures = supportedCultures;
});
}


您能否更详细地说明时间格式应该在哪里更改,以及取决于哪个参数?您如何确定某人的居住地?@Marco我已从请求标头更新了问题,您可以找到HTTP_ACCEPT_语言区域设置,并使用下面的代码DateTime myDate=DateTime.Now;字符串us=myDate.ToString新文化信息us;字符串uk=myDate.ToStringnew cultureinfo-GB;控制台;书写器;Console.WriteLineuk;它将给出完整的日期和时间。但是你只能花时间。我的客户的要求是根据区域设置更改时间格式,而不是更改chrome设置。chrome中的设置仅用于测试目的。切换到en-US查看您的Web应用程序如何根据区域设置显示不同的时间格式。
DateTime.Now.ToLongTimeString()
DateTime.Now.ToShortTimeString()