问题
I am using the tag in my Blazor app. But how can i format the date using my own culture ?
<InputDate class="form-control" @bind-Value="@ValidDate" @bind-Value:culture="nl-BE" @bind-Value:format="dd/MM/yyyy" />
regards Dieter
回答1:
The answer is @bind-Value:format
, like so
@page "/"
<EditForm Model="@CurrentPerson">
<InputDate @bind-Value="@CurrentPerson.DateOfBirth" @bind-Value:format="dd/MM/yyyy"/>
</EditForm>
@code
{
Person CurrentPerson = new Person();
public class Person
{
public DateTime DateOfBirth { get; set; } = DateTime.Now;
}
}
There is also the option of using bind-Value:culture
.
回答2:
Specifying a format for the date field type isn't recommended because Blazor has built-in support to format dates.
However, if you opt to specify a format, you can do that like this: @bind-value:format="yyyy-MM-dd"
(applicable to InputDate component) or
@bind:format="yyyy-MM-dd" (applicable to input tag)
You may also use the @bind:culture parameter to provide a System.Globalization.CultureInfo for parsing and formatting a value. Specifying a culture isn't recommended when using the date and number field types. date and number have built-in Blazor support that provides the required culture.
This is done automatically, without intervention of the user. However, if you want, for testing purposes to format the date using your own culture, you may use Localization as explained here: https://docs.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#localization
Hope this helps...
来源:https://stackoverflow.com/questions/57508706/how-to-format-the-date-using-local-culture-using-the-blazor-inputdate-tag