How to get DateTime from the Internet (external resource - not from the server)

How can I get the current time from the Internet (external resource - not from the server)?
Edited
Example from the website below:
http://www.timeanddate.com/worldclock
Cause
I redirect my pages to the lock page after one month (by checking DateTime.Now), and on this page the user must enter an activation code to return ...
for some security reasons, I want to get the current date / time from my server ...

early

+6
source share
5 answers

I think this will help you with this. Use the code below to get the date from the 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; } 
+11
source

You cannot, without using DateTime.Now . This is a property of the DateTime structure that cannot be overridden.

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

+3
source

Why not use HttpRequest to request a site’s RSS feed that gives world time? Is that what you mean?

0
source

Why do you want to get it from the Internet, you still get it from someone who is torn.

0
source

You can get DateTime using JavaScript

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

Check this article for a javascript date and time object

and look at this also JavaScript JavaScript Object - Fixed Dead Link

-1
source

Source: https://habr.com/ru/post/892653/


All Articles