How should I fetch TimeZoneInfo in a platform-agnostic way?

前端 未结 1 1524
耶瑟儿~
耶瑟儿~ 2020-12-11 18:08

I\'m working on porting a .NET Framework 4.5 app to .NET Core 2.0 and have started to do some testing on my Mac. When I run this code on .NET Core 2.0 on Windows it works fi

相关标签:
1条回答
  • 2020-12-11 18:26

    This is a known problem, and is tracked in dotnet/corefx#11897. There's no built-in solution yet.

    However, my TimeZoneConverter library can be used for this.

    TimeZoneInfo tzi = TZConvert.GetTimeZoneInfo("Pacific Standard Time");
    

    It works by first seeing if the time zone is present on the computer. If not, then it converts to the equivalent time zone in IANA format (America/Los_Angeles in this case), and trying to retrieve it with that instead. The opposite case also works, so you can use either format on any OS.

    0 讨论(0)
提交回复
热议问题