Search code examples
c#.netdatetimeutcdatediff

Difference in usage of DateTime.Now vs DateTime.UtcNow


As this Question's Answer from hightechrider mentions that code block as below is more right

var start = DateTime.Parse("08/10/2011 23:50:31").Utc;

if(start.AddMinutes(20) > DateTime.UtcNow)

then using as this by TimeSpan

var start = DateTime.Now;
var oldDate = DateTime.Parse("08/10/2011 23:50:31");

if(start - oldDate).TotalMinutes >= 20)

Here since the DateTime is executed and also parsed in the same culture then, How it will make difference ??

I am feeling very Phoney by this answer.


Solution

  • In a nutshell: UTC is a continuous, single-valued time scale, whereas local time is not continuous or single-valued. The primary reason is Daylight Savings Time, which doesn't apply to UTC. So UTC never jumps forward or back an hour, whereas local time does. And when it jumps backward, the same time value occurs twice.

    Making comparisons is best done using the continuous, single-valued time scale, unless you want to mess around with DST yourself. Even if you do, there is no way to distinguish between the first and second "2am" when Daylight Savings Time ends and the clocks are set back an hour.

    Technical note: even though UTC is continuous, it does have the occasional extra leap second inserted to keep up with the slowing down of the Earth's rotation. Those seconds are usually added at the end of the day and are listed with 60 seconds. So you'd have 23:59:59, 23:59:60, 00:00:00.