我正在开发基于最新 Core 3.1 的 Blazor 项目。
UI 文化正确显示日期和数字,如图所示。
但是当我使用 EditForm 时,数字和日期的格式并未按应有的格式设置。
所以这个代码是 EditForm 的一部分
<InputDate id="date" class="form-control" @bind-Value="@TaskObject.Date" />
所以在 EditForm 中它看起来像这样,这不是正确的区域性格式:
但是在用户界面中看起来像这样,这没问题:
当我刚接触 Blazor 时,我尝试在线阅读不同的内容以获取有关此问题的一些知识。
所以我厌倦了以下内容:
- I want to change date format of form control class type is date?
- How to format the date using local culture using the blazor <InputDate /> tag
运气不好。
然后我尝试阅读 this并发现this它不适用于 Core 3.1。
我的问题是,到底应该怎样做才能使 EditForm 像 UI 一样显示日期和数字,以及为什么 EditForm 会发生这种情况?
最佳答案
Why this happen for EditForm
内置InputDate
/InputNumber
被设计为文化不变组件。请参阅source code of InputDate和 InputNumber 。
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/