Datetime ASP.NET核心应用程序上的日期时间格式错误

Datetime ASP.NET核心应用程序上的日期时间格式错误,datetime,asp.net-core,format,culture,Datetime,Asp.net Core,Format,Culture,我想在ASP.NET核心应用程序上设置相同的日期时间格式(pl)。我在配置中使用pl区域性: services.Configure<RequestLocalizationOptions>(options => { var supportedCultures = new[] { new CultureInfo("pl-PL"),

我想在ASP.NET核心应用程序上设置相同的日期时间格式(pl)。我在配置中使用pl区域性:

        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("pl-PL"),
            };
            options.DefaultRequestCulture = new RequestCulture("pl-PL", "pl-PL");
            options.DefaultRequestCulture.Culture.DateTimeFormat.FullDateTimePattern = "dd.MM.yyyy HH:mm:ss";
            options.DefaultRequestCulture.Culture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
            options.DefaultRequestCulture.UICulture.DateTimeFormat.FullDateTimePattern = "dd.MM.yyyy HH:mm:ss";
            options.DefaultRequestCulture.UICulture.DateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
            

            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;

        });
它开始工作,但只是在本地(在使用波兰操作系统的计算机上)。我发现生产服务器上的区域性(“pl-pl”)必须不同(但我不知道为什么)。因此,我决定补充:

if (!DateTime.TryParseExact(dateStr, "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime date))
            {
                if (!DateTime.TryParseExact(dateStr, "dd.MM.yyyy HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                {
                    if (!DateTime.TryParseExact(dateStr, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
                    {
                        if (!DateTime.TryParse(dateStr, new CultureInfo("pl-PL"), DateTimeStyles.None, out date))
                        {
                            bindingContext.ModelState.TryAddModelError(bindingContext.ModelName, "Data powinna być w formacie 'dd.MM.yyyy HH:mm:ss'");
                            return Task.CompletedTask;
                        }
                    }
                }
            }
这个解决方案可行,但很难看。 同样重要的是,我注意到不同浏览器之间的数据格式看起来不同。 在Firefox上:Od:19.02.2021 Do:05.03.2021(正确) 在铬上:Od:2021-02-19 Do:2021-03-05(错误)

在表示层上返回日期的代码:

<div class="col-sm-3">
Od: @vehicleAvailability.DateFrom.ToShortDateString()
Do: @vehicleAvailability.DateTo.ToShortDateString()
</div>

Od:@vehicleAvailability.DateFrom.ToSortDateString()
Do:@vehicleAvailability.DateTo.ToShortDateString()

Chrome和Firefox的区别在您的情况下,它们发送不同的
接受语言
标题,您可以在网络选项卡中查看。如果您想显示特定的日期格式,请考虑在<代码>日期时间> ToStudio< /Cord>< /P>中指定区域性。
<!-- ToShortDateString() equals ToString("d") -->
<div class="col-sm-3">
Od: @vehicleAvailability.DateFrom.ToString("d", new CultureInfo("pl-PL")) 
Do: @vehicleAvailability.DateTo.ToString("d", new CultureInfo("pl-PL"))
</div>

Od:@vehicleAvailability.DateFrom.ToString(“d”,新文化信息(“pl”))
Do:@vehicleAvailability.DateTo.ToString(“d”,新文化信息(“pl”))

“生产服务器上的区域性('pl-pl')必须不同”-这到底意味着什么?您如何向api发出请求?从
表单
还是使用javascript?您是否可以添加api代码,返回数据以在Firefox上显示:Od:19.02.2021 Do:05.03.2021(正确)在Chrome:Od:2021-02-19 Do:2021-03-05(错误)@Alexander via form上。我知道这听起来很有趣。我添加了日志记录来检查生产服务器上的错误:在解析之前是dateStr,在解析之后是date.ToString()。dateString等于01.02.2013,但解析后的日期是错误的。@Alexander我在文章中添加了显示日期的代码。是否可以全局转换它?我想为应用程序中的所有位置设置相同的区域性。@marmite实际上,您发布的代码将应用程序限制为单个受支持的区域性,即
pl
,因此使用此设置时,不同的浏览器不会产生不同的结果。你有额外的本地化代码吗?
UseRequestLocalization
中还有其他内容吗?你说得对!我删除了“app.UseRequestLocalization();”,这有助于显示。但我在绑定方面仍然有问题:/
<!-- ToShortDateString() equals ToString("d") -->
<div class="col-sm-3">
Od: @vehicleAvailability.DateFrom.ToString("d", new CultureInfo("pl-PL")) 
Do: @vehicleAvailability.DateTo.ToString("d", new CultureInfo("pl-PL"))
</div>