2

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.

Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
  • 2
    Harsh, HighTechRider explains the (DST) issue very well in his two comments on [the question](http://stackoverflow.com/questions/7701151/date-comparison-how-to-check-if-20-minutes-have-passed). Was there some element of them that you didn't understand? Granted, it's a deceptively complicated issue. – Michael Petrotta Oct 10 '11 at 04:43
  • Besides what Michael has said, HighTechRider had not advised about parsing date/time - he is advising to store start as UTC date/time (`start = DateTime.UtcNow`). If you must store the date/time as string then include time zone offset - use format such as ISO 8601 (UTC). – VinayC Oct 10 '11 at 04:49
  • possible duplicate of [DateTime.Now vs. DateTime.UtcNow](http://stackoverflow.com/questions/62151/datetime-now-vs-datetime-utcnow) – Jon Schneider May 19 '15 at 14:45

2 Answers2

10

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.

Jeffrey Sax
  • 10,253
  • 3
  • 29
  • 40
  • 1
    hm why do we have to jump the time backwards, that I don't understand and what is `Daylight Savings Time` ?? – Harsh Baid Oct 10 '11 at 04:57
  • 1
    @Harsh, read [this Wikipedia article](http://en.wikipedia.org/wiki/Daylight_saving_time). 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."* – Michael Petrotta Oct 10 '11 at 05:01
  • I am accepting your answer because your answer was more clear for me to understand. – Harsh Baid Oct 10 '11 at 18:29
5

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.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179