DateTime.UtcNow and GETUTCDATE() discrepancy

c# dapper datetime sql-server

Question

Our production server has an issue where C# DateTime.UtcNow doesn't line up with SQL Server's GETUTCDATE() by about 27 seconds:

Console.WriteLine("C# UTC Date: " + DateTime.UtcNow);
Console.WriteLine("DB UTC Date: " + Connection.Query<DateTime>("SELECT GETUTCDATE()").Single());

C# UTC Date: 4/12/2015 10:47:54 PM 
DB UTC Date: 4/12/2015 10:48:21 PM

Running the same code on my local machine correctly shows the same time.

The time returned when running GETDATE() directly on the product server lines up with the server time.

What might be causing this and how could I go about fixing it?

Accepted Answer

The machine that is running the C# code and the machine that is running the SQL Server have a difference in the system clocks.

There will always be a slight difference, but a difference as large as this is caused by one or both machines failing to get the correct time from an internet source.

In the Date and Time settings, in the Internet Time tab, you find the settings for getting the time from an internet source, and you will see when the server last managed to get an update. If the time server that is chosen doesn't work properly, choose a different time server.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why