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



我想在ASP上设置相同的日期时间格式(pl-PL)。. NET Core应用程序。我在我的配置中使用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;
});

这还不够。我在确定日期方面仍有问题。01.02.2013生效为02.01.2013。所以我写了自定义binder:

public class DateTimeModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
// Try to fetch the value of the argument by name
var modelName = bindingContext.ModelName;
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
if (valueProviderResult == ValueProviderResult.None)
return Task.CompletedTask;
bindingContext.ModelState.SetModelValue(modelName, valueProviderResult);
var dateStr = valueProviderResult.FirstValue;
// Here you define your custom parsing logic, i.e. using "pl-PL" culture

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;
}

bindingContext.Result = ModelBindingResult.Success(date);
return Task.CompletedTask;
}
}

它开始工作了,但只是在本地(在拥有优秀操作系统的计算机上)。我认为文化("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(正确)On the Chrome: Od: 2021-02-19 Do: 2021-03-05(错误)

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

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

Chrome和Firefox之间的区别在你的情况下,他们发送不同的Accept-Language头,你可以检查它在网络选项卡。如果要显示特定的日期格式,请考虑在DateTime.ToString

中指定区域性
<!-- 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>

最新更新