How to format the date using local culture using the blazor <InputDate /> tag

橙三吉。 提交于 2020-03-23 12:38:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!