Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DateTime always displays as UTC #15776

Closed
RickStrahl opened this issue Jul 22, 2018 · 5 comments
Closed

DateTime always displays as UTC #15776

RickStrahl opened this issue Jul 22, 2018 · 5 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@RickStrahl
Copy link

DateTime values always display as UTC time, rather than local time even when explicitly forcing .ToLocalTime().

This:

Console.WriteLine(DateTime.Now.ToLocalTime().ToString("HH:mm"));

displays UTC time.

@Suchiman
Copy link
Contributor

This is a mono issue and should be reported on https://github.com/mono/mono
I was digging around a bit and figured the issue is that emscripten doesn't emulate /etc/localtime and friends, thus mono doesn't know the current timezone and fallbacks to UTC.
You can make it somewhat work by hacking the appropriate offset like this:

var field = typeof(TimeZoneInfo).GetField("local", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
field.SetValue(null, TimeZoneInfo.CreateCustomTimeZone("FAKE", TimeSpan.FromHours(2), "FAKE", "FAKE"));

But this doesn't consider DST or adjustment rules.
Newer browsers support zone = new Intl.DateTimeFormat().resolvedOptions().timeZone; to return, for example, Europe/Berlin which you could feed into NodaTime to retrieve the Tzdb timezone which would give you accurate results.

@ransagy
Copy link

ransagy commented Jul 22, 2018

I have this JS snippet as a workaround but it's not the best thing either:
new Date(new Date().toLocaleString("en-US", { timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone })).getTimezoneOffset() / 60 * -1;

@Andrzej-W
Copy link

Tracked here:
mono/mono#6368

@RickStrahl
Copy link
Author

Thanks all for clarification.

@jsakamoto
Copy link

[FYI] This is my workaround until mono/mono#6368 is fixed.

@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

6 participants