Custom Datetime culture localization in .Net

33 Views Asked by At

I am working on a .Net web api project v6.0 and now I'm running through a serious problem. When I have DateTime or DateOnly variables, .Net takes out -3 or more hours and messes up all the date for that variable.

For example: When I send a "Todays X employee attendance" request with a DateTime data in it and if the request is "12/04/2023 15:30" or "12/04/2023 03:30 PM", when this request comes to the backend it will take it as "12/03/2023 11:30" or "12/03/2023 11:30 PM". So this little thing causes a big trouble when it comes to these kind of services that I want to implement...

I have tried something like this:

    public static void ConfigureLocalization(this IServiceCollection services)
    {
        services.Configure<RequestLocalizationOptions>(options =>
        {
            options.DefaultRequestCulture = new RequestCulture("de");
            options.SupportedCultures = new List<CultureInfo> { new CultureInfo("de") }; 
            options.RequestCultureProviders.Clear();
        });
    }

but the thing is that I want to make it based on the user's timezone and not to set it a static one because I may have employees from Russia for example, which has 11 timezones !. That would be a total mess.

Do you have any experience with this kind of problem or can you think of any solution that might help me with this? Thank you.

1

There are 1 best solutions below

0
Arunjith ps On

All date operations from the backend should be performed in UTC format, both in the application logic and the database. For the frontend, display time based on the user's browser timezone. If a custom timezone is used, load the offset details into the local storage at the time of login to the application. Utilize this offset value to accurately present datetime values in the correct timezone.