How to get DateTime from the internet?

C# 4.0Datetime

C# 4.0 Problem Overview


How to get current date and time from internet or server using C#? I am trying to get time as follows:

public static DateTime GetNetworkTime (string ntpServer)
{
    IPAddress[] address = Dns.GetHostEntry(ntpServer).AddressList;

    if (address == null || address.Length == 0)
        throw new ArgumentException("Could not resolve ip address from '" + ntpServer + "'.", "ntpServer");

    IPEndPoint ep = new IPEndPoint(address[0], 123);
    return GetNetworkTime(ep);
}

I am passing server IP address as netServer, but it does not work properly.

C# 4.0 Solutions


Solution 1 - C# 4.0

For environments where port 13 is blocked, time from NIST can be web scraped as below,

public static DateTime GetNistTime()
{
    DateTime dateTime = DateTime.MinValue;

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi?lzbc=siqm9b");
    request.Method = "GET";
    request.Accept = "text/html, application/xhtml+xml, */*";
    request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
    request.ContentType = "application/x-www-form-urlencoded";
    request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
        StreamReader stream = new StreamReader(response.GetResponseStream());
        string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
        string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
        double milliseconds = Convert.ToInt64(time) / 1000.0;
        dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds).ToLocalTime();
    }

    return dateTime;
}

Solution 2 - C# 4.0

Here is code sample that you can use to retrieve time from NIST Internet Time Service

var client = new TcpClient("time.nist.gov", 13);
using (var streamReader = new StreamReader(client.GetStream()))
{
    var response = streamReader.ReadToEnd();
    var utcDateTimeString = response.Substring(7, 17);
    var localDateTime = DateTime.ParseExact(utcDateTimeString, "yy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
}

Solution 3 - C# 4.0

Here is a quick code to get the time from the header, works without the need of port 13

public static DateTime GetNistTime()
{
    var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
    var response = myHttpWebRequest.GetResponse();
    string todaysDates = response.Headers["date"];
    return DateTime.ParseExact(todaysDates, 
                               "ddd, dd MMM yyyy HH:mm:ss 'GMT'", 
                               CultureInfo.InvariantCulture.DateTimeFormat, 
                               DateTimeStyles.AssumeUniversal);
}

Solution 4 - C# 4.0

Things could go wrong. All implements of the code founded above are prone to errors. Sometimes, it works and sometimes it trows a WebExpection error message.

A better implementation:

        try{
            using (var response = 
              WebRequest.Create("http://www.google.com").GetResponse())
                //string todaysDates =  response.Headers["date"];
                return DateTime.ParseExact(response.Headers["date"],
                    "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                    CultureInfo.InvariantCulture.DateTimeFormat,
                    DateTimeStyles.AssumeUniversal);
        }
        catch (WebException)
        {
            return DateTime.Now; //In case something goes wrong. 
        }

Conclusion:

Having your web app depend on a service that provides accurate date information is critical. I have used one of the code founded here in my app and it really mess things up.

Solution 5 - C# 4.0

One more version of the same idea:

public static class InternetTime
{
    public static DateTimeOffset? GetCurrentTime()
    {
        using (var client = new HttpClient())
        {
            try
            {
                var result = client.GetAsync("https://google.com", 
                      HttpCompletionOption.ResponseHeadersRead).Result;
                return result.Headers.Date;
            }
            catch
            {
                return null;
            }
        }
    }
}

Here HttpCompletionOption.ResponseHeadersRead is used to prevent loading of the rest of the response, as we need only HTTP headers.

Use InternetTime.GetCurrentTime().Value.ToLocalTime() to get current local time.

Solution 6 - C# 4.0

Important: first check the avaible servers on NIST Internet Time Servers.

public static DateTime GetServerTime()
{
    var result = DateTime.Now;

    // Initialize the list of NIST time servers
    // http://tf.nist.gov/tf-cgi/servers.cgi
    string[] servers = new string[] {
        "time-c.nist.gov",
        "time-d.nist.gov",
        "nist1-macon.macon.ga.us",
        "wolfnisttime.com",
        "nist.netservicesgroup.com",
        "nisttime.carsoncity.k12.mi.us",
        "nist1-lnk.binary.net",
        "wwv.nist.gov",
        "time.nist.gov",
        "utcnist.colorado.edu",
        "utcnist2.colorado.edu",
        "nist-time-server.eoni.com",
        "nist-time-server.eoni.com"
    };
    
    Random rnd = new Random();
    
    foreach (string server in servers.OrderBy(x => rnd.NextDouble()).Take(9))
    {                
        try
        {
            // Connect to the server (at port 13) and get the response. Timeout max 1second
            string serverResponse = string.Empty;
            var tcpClient = new TcpClient(); 
            
            if (tcpClient.ConnectAsync(server, 13).Wait(1000))
            {
                using (var reader = new StreamReader(tcpClient.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 >= 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;
}

Solution 7 - C# 4.0

    public static Nullable<DateTime> GetDateTime()
    {
        Nullable<DateTime> dateTime = null;
        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.microsoft.com");
        request.Method = "GET";
        request.Accept = "text/html, application/xhtml+xml, */*";
        request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
        request.ContentType = "application/x-www-form-urlencoded";
        request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
        try
        {
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                string todaysDates = response.Headers["date"];

                dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                    System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat, System.Globalization.DateTimeStyles.AssumeUniversal);
            }
        }
        catch
        {
            dateTime = null;
        }
        return dateTime;
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhmlasnkView Question on Stackoverflow
Solution 1 - C# 4.0NemoView Answer on Stackoverflow
Solution 2 - C# 4.0Alex AzaView Answer on Stackoverflow
Solution 3 - C# 4.0glienartView Answer on Stackoverflow
Solution 4 - C# 4.0cerberusView Answer on Stackoverflow
Solution 5 - C# 4.0dodbrianView Answer on Stackoverflow
Solution 6 - C# 4.0Xtian11View Answer on Stackoverflow
Solution 7 - C# 4.0Prashant ManjuleView Answer on Stackoverflow