Date formatting behavior changes between Blazor Server and WebAssembly
0 reputation · 24 May 2022, 19:27 UTC
0 reputation · 24 May 2022, 19:27 UTC
Date rendering must remain consistent when a Blazor application switches between Blazor Server and Blazor WebAssembly hosting models. The precise goal is to determine whether a custom date format string supplied through a component parameter continues to be honored after the active CultureInfo changes at runtime.
Blazor Server formats dates using the server-side Thread.CurrentCulture, while WebAssembly uses the browser's locale. A custom format string is honored only when the current CultureInfo matches the expected locale; otherwise the invariant format is used. Culture preferences may also fail to persist across page navigation or reload in WebAssembly, and server-side time zone adjustments do not propagate to the client.
35775 reputation · 25 May 2022, 03:48 UTC
1. When does Blazor fall back to the invariant format?
CultureInfo changes. The ToString(format) call uses whatever culture is current on the executing thread.BlazorWebAssemblyLoadAllGlobalizationData), only the invariant culture is available. A custom format that relies on locale‑specific names will then be rendered using the invariant pattern.2. Is formatting consistent across Server and WebAssembly without extra JS interop?
Thread.CurrentCulture (often derived from the incoming request or a RequestLocalization middleware). WebAssembly defaults to the browser’s locale, and without full globalization data it may fall back to invariant.3. How to preserve a user‑selected culture across navigation and reloads?
localStorage or a cookie. Include it in the URL query string if you want to avoid storage.Program.cs for WASM or Startup.cs for Server), read the persisted value and set CultureInfo.CurrentCulture and CultureInfo.CurrentUICulture before the first component renders.CircuitHandler or by configuring RequestLocalization to read the cookie on each request.BlazorWebAssemblyLoadAllGlobalizationData=true to the project file) so that the persisted culture can be fully respected.CultureInfo, so the persisted culture remains active. A full page reload will re‑apply the culture during startup, preserving consistency.Diagnostic detail needed
To fine‑tune the recommendation, could you confirm which .NET runtime version you are targeting for WebAssembly and whether BlazorWebAssemblyLoadAllGlobalizationData is enabled? This determines if the client can use non‑invariant cultures.
Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 25 May 2022, 03:37 UTC
A custom format string like MMM dd, yyyy uses the current culture's DateTimeFormatInfo; it does not automatically fall back to invariant output when the culture changes. However, Blazor WebAssembly only includes invariant globalization data by default. Unless BlazorWebAssemblyLoadAllGlobalizationData is enabled in the project file (or specific cultures are opted into via InvariantGlobalization false with satellite assemblies), non-invariant cultures will fall back to invariant formatting at runtime, producing different output than Blazor Server for the same format string. This behavior varies by .NET SDK version (significant changes in .NET 6 through 8), so verify the target framework rather than assuming full ICU support.