Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localizing DateTime (and numbers) in Blazor

I am working on Blazor project based on latest Core 3.1.

The UI Culture show correctly dates and numbers as seen the in the image.

But the moment I used the EditForm the number and date is not formatted as it should be.

So this code part of EditForm

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

So in EditForm it looks like this, which is not correct culture format:

enter image description here

But in the UI looks like this, which is OK:

enter image description here

As I new in Blazor, I have tried to read different stuff online to get some knowledge regarding this issue.

So I have tired following:

  • 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

without luck.

Then I tried to read this and found this which is not working with Core 3.1.

My question, what should exactly be done to make EditForm show date and number like the of UI, and Why this happen for EditForm?

like image 455
Maytham Avatar asked Dec 29 '19 20:12

Maytham


2 Answers

Why this happen for EditForm

The built-in InputDate/InputNumber is designed as Culture-Invariant components. See source code of InputDate and InputNumber.

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

I thought we can create a custom InputDate<TValue> implementation. However, I was wrong. According to 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..

Even we get a custom InputDate<TValue>implementation that honors the current CultureInfo, we still need some js/css to display the correct format. IMO, there's no standard way to implement this. See also this thread on SO.

like image 103
itminus Avatar answered Oct 09 '22 06:10

itminus


This is not a Blazor issue but rather the behaviour of the HTML <input> element of type="date".

The required format for type="date" is "yyyy-MM-dd" which the Blazor component uses. Any other format is not valid.

If we do a little test, we can verify it is not a Blazor issue.

@page "/dates"
@using System.Globalization
<h3>Date</h3>
<p>@_dateString</p>
<input type="date" value="@_dateString" />
@code {
    private string _dateString;

    protected override void OnInitialized()
    {
        // using en-US culture
        // this is what InputDate component does
        _dateString = DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture);
    }
}

_dateString outputs 2019-12-30 but the date shown in the <input> is 30/12/2019

Detailed information on type=date can be found here. In the Value section there is a note which says the following:

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

So the formatting is fixed to the locale of the users browser.

like image 2
Colin Bacon Avatar answered Oct 09 '22 08:10

Colin Bacon