jdtcn / BlazorDateRangePicker

A Blazor component for choosing date ranges and dates
MIT License
186 stars 34 forks source link

How can I make the control use the date format of the current culture, assuming the culture is correct? #63

Closed szalapski closed 2 years ago

szalapski commented 3 years ago

When I'm in en-GB culture, I'd expect to see dd/MM/yyyy date format, but instead I see M/d/yyyy. What could be wrong here?

<text>@System.Globalization.CultureInfo.CurrentCulture</text>

<DateRangePicker 
    @ref="Picker" 
    class="form-control"
    MinDate="Filters.FiscalYearStartDate"
    MaxDate="Filters.FiscalYearEndDate.AddDays(1)"
    SingleDatePicker="@Filters.LimitToOneDate"
    OnRangeSelect="SendFiltersChanged" 
    OnMonthChanged="MonthChanged" 
    DaysEnabledFunction="DaysEnabledFunction"
    @bind-StartDate="Filters.StartDateRangeBegin"
    @bind-EndDate="Filters.StartDateRangeEnd"
    @onchange="SendFiltersChanged">
    <ButtonsTemplate>...</ButtonsTemplate></DateRangePicker>

results in output:

image

szalapski commented 3 years ago

I suspect this might be related to this: https://stackoverflow.com/questions/69542125/in-blazor-webassembly-in-a-component-how-can-i-make-datetime-tostring-use-the-d/69545067#69545067

All WebAssembly component outputs show me that DateTimeFormatInfo.CurrentInfo.ShortDatePattern == "M/d/yyyy" even though CultureInfo.CurrentCulture.Name == "en-GB". Why might that be? What sets ShortDatePattern from the culture and can I "re-initialize" it?

jdtcn commented 3 years ago

Hi Patrick,

It looks like they have the wrong format in the WebAssembly runtime for the en-GB culture, but I checked it on version 6.0.0rc2, the correct format dd/MM/yyyy is set there, you can try to update. This is not a problem anyway, the date format can be enforced using the DateFormat property.

szalapski commented 3 years ago

Thanks; that's surprising. I can't go live on version 6 till it is released. It isn't fixed in any version 5 package?

Where is DateFormat documented? I don't see it in the Readme. I tried to supply it with "dd/MM/yyyy" and it gave me this: image

jdtcn commented 3 years ago

Very strange, I have never seen such a thing. What dotnet version and what kind of browser do you have? It turned out that the DateFormat parameter is not in the documentation, I will add it, thanks for paying attention.

You can also take full control of the picker rendering with the PickerTemplate but this is of course not very convenient.

Later I will check how the picker behaves in the latest dotnet 5 release.

jdtcn commented 3 years ago

Checked dotnet 5.0.10 - the format for en-GB is wrong there, in 6.0.0 it is fixed. But DateFormat works for me on version 5.0.10, I checked it like this:

<DateRangePicker Culture="@(CultureInfo.GetCultureInfo("en-GB"))" DateFormat="dd/MM/yyyy" />