0

I have a problem when I visualize dates in my application.

I have a asp.net core application. I have razor pages. 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.

We do not have this problem in input tag:

<input type="date" asp-for="@item.MyDate"  />

Obviously it work because in my input tag I set the type=date. If I set

<input type="text" asp-for="@item.MyDate"  />

It would not work anymore.

Now I have the problem with labels and span.. I cannot set the type of the data in those tags. So the date is always shown in English.

How can I solve this problem? Exists a way to solve it without js or without modifying the server?

Thank you

2
  • @MdFaridUddinKiron, I have been still applying the solution. Now I have problem on input text.. I don't receive any error, but numbers are saved in a wrong way.. for example if I write 1,300.00, it becomes 130,000.00 .. i think there is some problem with some script that add thousands separator Commented Oct 27, 2022 at 9:01
  • 1
    Provided solution only would work with the date format, other stuff should not be impacted as you can see the code 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. Commented Oct 27, 2022 at 9:21

1 Answer 1

1

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

enter image description here

Note: If you need any further assistance you could refer to following guideline.

  1. How you can set user-culture
  2. Inside-out user culture
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.