c# - 在 Blazor 中本地化日期时间(和数字)

标签 c# asp.net-core localization blazor

我正在开发基于最新 Core 3.1 的 Blazor 项目。

UI 文化正确显示日期和数字,如图所示。

但是当我使用 EditForm 时,数字和日期的格式并未按应有的格式设置。

所以这个代码是 EditForm 的一部分

<InputDate id="date" class="form-control" @bind-Value="@TaskObject.Date" />

所以在 EditForm 中它看起来像这样,这不是正确的区域性格式:

enter image description here

但是在用户界面中看起来像这样,这没问题:

enter image description here

当我刚接触 Blazor 时,我尝试在线阅读不同的内容以获取有关此问题的一些知识。

所以我厌倦了以下内容:

运气不好。

然后我尝试阅读 this并发现this它不适用于 Core 3.1。

我的问题是,到底应该怎样做才能使 EditForm 像 UI 一样显示日期和数字,以及为什么 EditForm 会发生这种情况?

最佳答案

Why this happen for EditForm

内置InputDate/InputNumber被设计为文化不变组件。请参阅source code of InputDateInputNumber

What should exactly be done to make EditForm show date and number like the of UI,

我想我们可以创建一个自定义InputDate<TValue>执行。然而,我错了。根据MDN :

The displayed date is formatted based on the locale of the user's browser, but the parsed value is always formatted yyyy-mm-dd..

即使我们得到了定制InputDate<TValue>尊重当前CultureInfo的实现,我们还需要一些js/css来显示正确的格式。 IMO,没有标准的方法来实现这一点。另请参阅this thread on SO .

关于c# - 在 Blazor 中本地化日期时间(和数字),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59523054/

相关文章:

vb.net - 如何在 VB.NET 中单击按钮切换表单语言?

c# - 使用未分配的局部变量? C#

c# - 我可以将 libxml2 导入 Visual Studio 2013

c# - 在Asp.Net Core 2.0中使用AllowHtml从数据库生成网页

c# - 在 ASP.NET 5 中从 config.json 中检索部分

.net - .NET 中的资源文件、string.Format 和占位符

javascript - 如何实现本地化 javascript?

c# - 从c#生成Word文档

c# - Azure DevOps 和 Azure Function 部署 : where are my app settings

asp.net-core - IdentityServer4 签名证书引用 token