Difference in usage of DateTime.Now vs DateTime.UtcNow

15,634

Solution 1

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.

Solution 2

The United States transitions from Daylight Savings Time to Standard Time at 2AM on November 6th, 2011. If, at 2:10AM, I ask how far in the past 1:50AM was, .NET will tell me 20 minutes. In truth, it was an hour and 20 minutes, since we set our clocks back an hour at 2AM. I won't run into these issues if I use UTC - libraries like the .NET Framework have all the logic needed to correctly deal with discontinuities like this.

The whole Daylight Savings Time scheme is a mess, and it's hard for anyone whose country, like yours, (sensibly) doesn't implement it, to understand the issues that arise. It gets even more interesting when governments start changing the switchover days around.

Share:
15,634
Harsh Baid
Author by

Harsh Baid

:: Senior Sitecore Developer :: Accomplishments Microsoft Certifications MCP - TS: Microsoft .NET Framework - Application Development Foundation (70-536) MCTS - TS: Windows Applications Development with Microsoft .NET Framework 4 (70-511) Sitecore Certifications Sitecore CMS 6.5 (SND) Sitecore DMS 2.0 (DMC) Sitecore DMS v7.2 (DMS.NET) Interests Web Development Windows Development Mobile Developement Cloud Development Languages I love to work in C#, Java Technologies Worked Till ASP.NET AJAX and AJAX Control Toolkit Jquery and JavaScript Visual Studio and SQL Server ADO.NET UML Web Services & WCF Service WPF with MVVM Winforms @baid_harshgooglefacebooklinkedin Sitecore Community Profile

Updated on June 14, 2022

Comments

  • Harsh Baid
    Harsh Baid almost 2 years

    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.

  • Harsh Baid
    Harsh Baid over 12 years
    hm why do we have to jump the time backwards, that I don't understand and what is Daylight Savings Time ??
  • Michael Petrotta
    Michael Petrotta over 12 years
    @Harsh, read this Wikipedia article. Excerpt: "Daylight Saving Time (DST) is the practice of temporarily advancing clocks during the summertime so that afternoons have more daylight and mornings have less. Typically clocks are adjusted forward one hour near the start of spring and are adjusted backward in autumn."
  • Harsh Baid
    Harsh Baid over 12 years
    I am accepting your answer because your answer was more clear for me to understand.