Blazor webassembly has a bug, it always use UTC time as your local timezone. So no matter what the timezone you are in, .ToLocalTime()
will always returning UTC time to you.
In order to show the correct local time to the user, we need manually adjust the time based on the timezone offset which we can get from javascript. The code looks like this:
1 | public class Program |
One thing is worth to mention, the javascript code new Date().getTimezoneOffset()
returns UTC - localtime
in minutes, so we use -
before it to get the delta of localtime - UTC
which can be directly used in AddMinutes()
.