A fully-featured and lightweight NTP (Network Time Protocol) client for .NET.
$ dotnet add package NetworkTime
A fully-featured and lightweight NTP (Network Time Protocol) client for .NET.
Supports both synchronous and asynchronous APIs, as well as round-trip delay and local time offset correction.
dotnet add package NetworkTime
Or via the NuGet Package Manager:
Install-Package NetworkTime
var client = new NtpClient();
DateTime localTime = await client.GetNetworkTimeAsync();
Console.WriteLine("NTP Time (Local): " + localTime);
var client = new NtpClient("time.windows.com");
if (client.TryGetNetworkTime(out DateTime ntpTime))
Console.WriteLine("Time from NTP: " + ntpTime);
else
Console.WriteLine("Failed to get NTP time.");
var result = await client.GetFullNetworkTimeAsync();
Console.WriteLine($"Server: {result.Server}");
Console.WriteLine($"UTC Time: {result.UtcTime}");
Console.WriteLine($"Round-Trip Delay: {result.RoundTripDelay.TotalMilliseconds} ms");
Console.WriteLine($"Local Offset: {result.LocalOffset.TotalMilliseconds} ms");
NtpClient| Method | Description |
|---|---|
Task<DateTime> GetNetworkTimeAsync() | Gets local time from NTP (async) |
DateTime GetNetworkTime() | Gets local time from NTP (sync) |
bool TryGetNetworkTime(out DateTime) | Tries to get local NTP time safely |
Task<TimeSpan> GetOffsetFromLocalAsync() | Returns offset between system and NTP clock |
Task<NtpResult> GetFullNetworkTimeAsync() | Returns detailed result with delay info |
NtpResultContains:
This project is licensed under the MIT License.
See the LICENSE file for details.