Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get DateTime from the internet?

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.

like image 895
hmlasnk Avatar asked Jun 22 '11 05:06

hmlasnk


People also ask

How do I get a timestamp from the Internet?

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

How to get current Date from internet in c#?

DateTime d = DateTime. UtcNow; DateTimeOffset dto2 = DateTimeOffset.

How do I convert DateTime to time?

You can't change a DateTime value - it's immutable. However, you can change the variable to have a new value. The easiest way of doing that to change just the time is to create a TimeSpan with the relevant time, and use the DateTime.


2 Answers

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; } 
like image 192
Nemo Avatar answered Sep 22 '22 06:09

Nemo


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); } 
like image 35
Alex Aza Avatar answered Sep 20 '22 06:09

Alex Aza