How to synchronize DateTime.Now with an NTP server?

I have an NTP server and I get the date as shown below:

DateTime NTPDateTime = GetServerTime(); //GetServerTime returns a new DateTime

but my problem is that I get the current time from the ntp server, but I canโ€™t update it without reconnecting with the NTP server.

I tried to do this in a stream like (1000 ms interval)

string displayTime() {
   this.lblServerTime.Text = NTPDateTime.ToLongTimeString();
}

It always shows the same DateTime as expected. How (01.10.2014 - 15:31:25)

But how can I update the DateTime so that it always gives me the current DateTime?

Example, if I use the following code, it gives me the current local time

//in a thread
string displayTime() {
   this.lblServerTime.Text = DateTime.Now.ToLongTimeString();
}

But I need server time, not localtime. and my problem is that I canโ€™t get it everysecond.

Is there a way to do this, without a new connection every second to the ntp server? I have over 500 client applications that require server time.

+4
1

. , , .

- NTP- " ".

DateTime.Now , .

, , - , .

TimeSpan serverTimeDifference = GetServerTimeDifference();

DateTime actualTime = DateTime.Now + serverTimeDifference;

GetServerTimeDifference:

private static TimeSpan GetServerTimeDifference()
{
    return GetServerTime() - DateTime.Now;
}
+4

All Articles