VB.NET - 查找当前时区,然后转换为太平洋

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14312952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:52:44  来源:igfitidea点击:

VB.NET - Find current timezone, then convert to Pacific

vb.netdatetime

提问by eqiz

I tried searching around for the code, but I didn't have too much luck. I found little snippets and I know how to convert current time to UTC, but I just really want to know how to get the current time zone, then convert the time to pacific.

我尝试四处寻找代码,但运气不佳。我发现了一些小片段,我知道如何将当前时间转换为 UTC,但我真的很想知道如何获取当前时区,然后将时间转换为太平洋时间。

I have many users that use my software all the way from california to maine. So I need to find out what timezone they are in, then convert it to pacific no matter where they are so I can accurately compare the local time to my servers time.

我有很多用户从加利福尼亚州到缅因州一路使用我的软件。所以我需要找出他们所在的时区,然后将其转换为太平洋,无论他们在哪里,这样我就可以准确地将当地时间与我的服务器时间进行比较。

回答by John Koerner

You can use the TimeZoneInfo class to convert time zones for you:

您可以使用 TimeZoneInfo 类为您转换时区:

    Dim now As DateTime = DateTime.Now
    MessageBox.Show(now.ToString())

    Dim pacificNow = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))
    MessageBox.Show(pacificNow.ToString())

You can read more about the TimeZones and the Ids on MSDN.

您可以在MSDN上阅读有关 TimeZones 和 Id 的更多信息