title: How to get country wise / timezone wise date & time from internet server
body: <p>i have to show date &amp; time to user as per the user timezone id. i can not rely on user pc date time and setting. i was searching Google for the way and got a below code. code seems will query many external server for date &amp; time but one thing not clear to me what kind of date &amp; time i can expect from the below code.</p>
  
  <p>some time i need india time. some time i need london time and some time i need other country local time. few county has many timezone that is why i want to send timezone id/name and want to get datetime based on timezone id/name.</p>
  
  <p>so just guide me how to customize the below code where i can send user's side timezone id and routine will return right local time based on timezone id even if user pc datetime setting is wrong. looking for good help. thanks</p>
  
  <pre><code>public static DateTime GetFastestNISTDate()
  {
      var result = DateTime.MinValue;
  
      // Initialize the list of NIST time servers
      // http://tf.nist.gov/tf-cgi/servers.cgi
      string[] servers = new string[] {
          "nist1-ny.ustiming.org",
          "nist1-nj.ustiming.org",
          "nist1-pa.ustiming.org",
          "time-a.nist.gov",
          "time-b.nist.gov",
          "nist1.aol-va.symmetricom.com",
          "nist1.columbiacountyga.gov",
          "nist1-chi.ustiming.org",
          "nist.expertsmi.com",
          "nist.netservicesgroup.com"
  };
  
          // Try 5 servers in random order to spread the load
          Random rnd = new Random();
          foreach (string server in servers.OrderBy(s =&gt; rnd.NextDouble()).Take(5))
          {
              try
              {
                  // Connect to the server (at port 13) and get the response
                  string serverResponse = string.Empty;
                  using (var reader = new StreamReader(new System.Net.Sockets.TcpClient(server, 13).GetStream()))
                  {
                      serverResponse = reader.ReadToEnd();
                  }
  
                  // If a response was received
                  if (!string.IsNullOrEmpty(serverResponse))
                  {
                      // Split the response string ("55596 11-02-14 13:54:11 00 0 0 478.1 UTC(NIST) *")
                      string[] tokens = serverResponse.Split(' ');
  
                      // Check the number of tokens
                      if (tokens.Length &gt;= 6)
                      {
                          // Check the health status
                          string health = tokens[5];
                          if (health == "0")
                          {
                              // Get date and time parts from the server response
                              string[] dateParts = tokens[1].Split('-');
                              string[] timeParts = tokens[2].Split(':');
  
                              // Create a DateTime instance
                              DateTime utcDateTime = new DateTime(
                                  Convert.ToInt32(dateParts[0]) + 2000,
                                  Convert.ToInt32(dateParts[1]), Convert.ToInt32(dateParts[2]),
                                  Convert.ToInt32(timeParts[0]), Convert.ToInt32(timeParts[1]),
                                  Convert.ToInt32(timeParts[2]));
  
                              // Convert received (UTC) DateTime value to the local timezone
                              result = utcDateTime.ToLocalTime();
  
                              return result;
                              // Response successfully received; exit the loop
  
                          }
                      }
  
                  }
  
              }
              catch
              {
                  // Ignore exception and try the next server
              }
          }
          return result;
      }
  </code></pre>
  
  <h2>EDIT</h2>
  
  <pre><code>    var wc = GetFastestNISTDate();
  
      var pattern = InstantPattern.CreateWithInvariantCulture("dd/MM/yyyy HH:mm:ss");
      var parseResult = pattern.Parse(wc.ToString("dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture));
      if (!parseResult.Success)
          throw new InvalidDataException("...whatever...");
      var instant = parseResult.Value;
  
      var timeZone = DateTimeZoneProviders.Tzdb["Europe/London"];
      var zonedDateTime = instant.InZone(timeZone);
      var bclDateTime = zonedDateTime.ToDateTimeUnspecified();
  </code></pre>
  
  <p>timezone translation is not working. i got the right date from this function <code>GetFastestNISTDate();</code> and next i try to get local date and time of different timezone based on my first utc time but code return wrong time for London. i guess i am making mistake the code. can anyone see &amp; help. thanks</p>