C# 如何检查当前时间是否在美国时区

C# 如何检查当前时间是否在美国时区,c#,C#,我可以使用DateTime=DateTime.Now获得当前时间;但我想将此时间与TimeZoneInfo进行比较,以获得与美国提供的UTC偏移相同的时间 基本上,我想知道提供的时间是在与美国相关的六个时区之一,包括夏威夷和阿拉斯加。如果我了解你的问题,你可以从互联网上获得。您可以检查下面的函数 DateTime DT = GetNISTDate(true); MessageBox.Show(DT.ToString()); public static D

我可以使用DateTime=DateTime.Now获得当前时间;但我想将此时间与TimeZoneInfo进行比较,以获得与美国提供的UTC偏移相同的时间


基本上,我想知道提供的时间是在与美国相关的六个时区之一,包括夏威夷和阿拉斯加。

如果我了解你的问题,你可以从互联网上获得。您可以检查下面的函数

        DateTime DT = GetNISTDate(true);
        MessageBox.Show(DT.ToString());

    public static DateTime GetNISTDate(bool convertToLocalTime)
    {
        Random ran = new Random(DateTime.Now.Millisecond);
        DateTime date = DateTime.Today;
        string serverResponse = string.Empty;
        // Represents the list of NIST servers  
        string[] servers = new string[] {  
                     "64.90.182.55",  
                     "206.246.118.250",  
                     "207.200.81.113",  
                     "128.138.188.172",  
                     "64.113.32.5",  
                     "64.147.116.229",  
                     "64.125.78.85",  
                     "128.138.188.172" 
                      };

        // Try each server in random order to avoid blocked requests due to too frequent request
        for (int i = 0; i < 5; i++)
        {
            try
            {
                // Open a StreamReader to a random time server  
                StreamReader reader = new StreamReader(new System.Net.Sockets.TcpClient(servers[ran.Next(0, servers.Length)], 13).GetStream());
                serverResponse = reader.ReadToEnd();
                reader.Close();

                // Check to see that the signiture is there  

                if (serverResponse.Length > 47 && serverResponse.Substring(38, 9).Equals("UTC(NIST)"))
                {
                    // Parse the date  
                    int jd = int.Parse(serverResponse.Substring(1, 5));
                    int yr = int.Parse(serverResponse.Substring(7, 2));
                    int mo = int.Parse(serverResponse.Substring(10, 2));
                    int dy = int.Parse(serverResponse.Substring(13, 2));
                    int hr = int.Parse(serverResponse.Substring(16, 2));
                    int mm = int.Parse(serverResponse.Substring(19, 2));
                    int sc = int.Parse(serverResponse.Substring(22, 2));

                    if (jd > 51544)
                        yr += 2000;
                    else
                        yr += 1999;
                    date = new DateTime(yr, mo, dy, hr, mm, sc);

                    // Convert it to the current timezone if desired  
                    if (convertToLocalTime)
                        date = date.ToLocalTime();
                    // Exit the loop  
                    break;
                }
            }
            catch
            {
                /* Do Nothing...try the next server */
            }
        }
        return date;
    }

听起来你已经知道你需要什么了

var currentTime=DateTime.Now;返回当前系统时间

var zone=TimeZoneInfo.Local;将为您提供当前系统时区。然后,您需要将时区与美国时区进行比较,在标准冬季时间UTC-5到UTC-10,夏令时时间UTC-4到UTC-10


然而,只要看看你的评论,仅仅因为时间在美国的某个时区之内并不意味着它在美国。北美和南美有很多国家都有相同的时区。

这应该可以满足您的需求,并作为如何获得其他时区的示例

class Program
{
    static void PrintTime(string timeZoneId)
    {
        Console.WriteLine( 
            TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, 
                timeZoneId).ToShortTimeString() +
            " is the time in " + timeZoneId);
    }

    public static void Main()
    {
        PrintTime("Hawaiian Standard Time");
        PrintTime("Alaskan Standard Time");
        PrintTime("Pacific Standard Time");
        PrintTime("US Mountain Standard Time");
        PrintTime("Central Standard Time");
        PrintTime("US Eastern Standard Time");
    }
}

美国,包括夏威夷和阿拉斯加,至少有6个时区宽。你指的只是美国大陆吗?美国并没有真正的时区;你真正想知道的是感兴趣的时间是否在UTC-5到UTC-10之间(包括UTC-5和UTC-10)。请注意,这是6个时区。如果不包括夏威夷和阿拉斯加,那么UTC-5到UTC-8之间有4个区域。我们能提供什么帮助?听起来你已经知道你想做什么了。。。去做吧,做些修改。。让我知道这是否正确。@vir_28您应该恢复编辑,并为新问题创建新问题,而不是编辑原始问题。您不应编辑问题以更改其全部含义:-这就是我想要暗示的;但我不熟悉TimeZoneInfo+1荣誉;我试图用DateTime.Now获得偏移量。ToUniversalTime@psubsee2003,我同意!说真的,我有麻烦了!!我正在windows phone 7中创建一个应用程序,我想在那里显示当前的国家名称@vir_28我从未做过任何Win7手机开发,但我认为访问GPS信息和进行反向地理编码以获得location@psubsee2003,这将使我的应用程序更复杂、更好,我将把我的应用程序限制在少数几个市场国家:P,并将进行更多的研发