how can get DateTime From Internet (External Resource - Not From Server)

亡梦爱人 提交于 2020-01-19 10:12:45

问题


how can i get current time From Internet (External Resource - Not From Server)?
Edited
For Example From the Below WebSite :
http://www.timeanddate.com/worldclock
Reason
i will redirect my pages to a Lock page after one month (by checking DateTime.Now) and in that page user should input activation code for comming back...
for some security reasons i want to get the current Date/Time From Out Of My Server...

thanks in advance


回答1:


I think this will help you out of it.Apply the below mentioned code for retrieving the date from Internet.

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 => 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 >= 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;
    }



回答2:


You can't, not by using DateTime.Now. It is a property of the DateTime structure that can't be overridden.

You can use an NTP server to get time (though there is not support in the BCL for one).




回答3:


Why not use a HttpRequest to query the RSS feed of a site which gives world time? Is this what you mean?




回答4:


Why would you want to get it from the internet, you would still be getting it from someone's sever.




回答5:


You can get the DateTime using JavaScript

    myDate = new Date();
    myday = myDate.getDay();
    mymonth = myDate.getMonth();
    myweekday= myDate.getDate();

Check this article to javascript date and time object

and look at this as well JavaScript Date Object - Dead link fixed



来源:https://stackoverflow.com/questions/6676191/how-can-get-datetime-from-internet-external-resource-not-from-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!