Reputation: 3068
My application is hosted by a server located in the US. I live in the UK.
When I open the page locally I can see the proper format of the date dd/mm/yyyy
based on the place I live, however, when I deploy the app to Azure US app service, and open it in the same web browser I can see the DateTime
format changed to the US culture mm/dd/yyyy
The part of the code from the razor page .cshtml:
<br>
<h2>Subscription</h2>
<hr />
<dl class="row">
<dt class="col-sm-3">
@Html.DisplayNameFor(model => model.Subscription.StartDate)
</dt>
<dd class="col-sm-9" id="StartDate">
@Html.DisplayFor(model => model.Subscription.StartDate)
</dd>
...
<dt class="col-sm-3">
@Html.DisplayNameFor(model => model.Subscription.EndDate)
</dt>
<dd class="col-sm-9" id="EndDate">
@Html.DisplayFor(model => model.Subscription.EndDate)
</dd>
</dl>
I played with changing the way how I display the date, I had an idea to apply CultureInfo.InvariantCulture
and I ended up with sth. like
@(Model.Subscription.EndDate.ToString(CultureInfo.InvariantCulture))
but this still doesn't help. I understand that the page is generated on the server, however, is there any way how I can display the StartDate
/EndDate
based on the browser's settings rather than server once?
Maybe I need to use some javascript extension to achieve what I want?
Is it possible to define a global rule to display all the dates in the client's format for the whole RazorPages application?
Cheers
Upvotes: 1
Views: 568
Reputation: 49095
What you're looking for is the equivalent of enableClientBasedCulture
in classic ASP.NET:
<system.web>
<globalization enableClientBasedCulture="true" ... />
</system.web>
For modern ASP.Net applications (that are Owin-based) you need to rely on the Localization middleware to achieve (roughly) the same results.
In your Startup.cs
:
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
});
Some differences though:
The classic enableClientBasedCulture
only uses the HTTP Accept
header to determine the culture, while the new middleware tries from the querystring and cookies as well.
The classic enableClientBasedCulture
would map to any requested culture that is supported by the OS, while the new middleware requires all supported cultures to be pre-defined.
For a more detailed discussion, see https://github.com/dotnet/aspnetcore/issues/2649
Upvotes: 1