The culture set in my server is English, so the date format is
mm-dd-yyyy. Now we have users in USA and in Italy. So dates must be
shown in different ways: "mm-dd-yyyy" in USA and "dd-mm-yyyy" in
Italy.
In this scenario, firstly, you should get the user-culture, or user browser's language even we can consider http request header as well.
Based on that, we can decide which date format should display in view. Using Custom Middleware we can achieve that. I can provide an work around showing the middleware example
DateFormatterMiddleware:
public class DateFormatterMiddleware
{
private readonly RequestDelegate next;
public DateFormatterMiddleware(RequestDelegate next) { this.next = next; }
public async Task InvokeAsync(HttpContext context)
{
var userLanguages = context.Request.Headers.AcceptLanguage;
var selectLan = userLanguages.ToString();
var languages = selectLan.Split(',')
.Select(StringWithQualityHeaderValue.Parse)
.OrderByDescending(s => s.Quality.GetValueOrDefault(1));
var userLan = languages.Select(val => val.Value).ToList();
foreach (var item in userLan)
{
DateTime currentDate = DateTime.Now;
if (item.Contains("en-US") || item.Contains("it-IT"))
{
if (item == "en-US")
{
var enUS_Format = currentDate.ToString("MM-dd-yyyy", CultureInfo.CreateSpecificCulture("en-US"));
context.Session.SetString("DateFormat", enUS_Format);
break;
}
if (item == "it-IT")
{
var italy_Format = currentDate.ToString("dd-MM-yyyy", CultureInfo.CreateSpecificCulture("it-IT"));
context.Session.SetString("DateFormat", italy_Format);
break;
}
}
}
await next(context);
}
}
Resgister DateFormatterMiddleware In Program.cs:
builder.Services.AddSession();
app.UseSession();
Use DateFormatterMiddleware In Program.cs:
app.UseMiddleware<DateFormatterMiddleware>();
View
@inject IHttpContextAccessor _httpcontext;
@{
var data = _httpcontext.HttpContext?.Session.GetString("DateFormat")?.ToString();
}
<h3>User Current Date Format :@data</h3>
Output

Note: If you need any further assistance you could refer to following guideline.
- How you can set
user-culture
- Inside-out
user culture
date format, other stuff should not be impacted as you can see thecode snippet. Please check the other relevant code as well. If the provided solution could resolve your current concern, It would be great if you could post new question for new issue, then this questions would be unique.