Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6206747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:30:12+00:00 2026-05-24T05:30:12+00:00

I just noticed what seems like a ridiculous flaw with DateTime comparison. DateTime d

  • 0

I just noticed what seems like a ridiculous flaw with DateTime comparison.

DateTime d = DateTime.Now;
DateTime dUtc = d.ToUniversalTime();

d == dUtc; // false
d.Equals(dUtc); //false
DateTime.Compare(d, dUtc) == 0; // false

It appears that all comparison operations on DateTimes fail to do any type of smart conversion if one is DateTimeKind.Local and one is DateTimeKind.UTC. Is the a better way to reliably compare DateTimes aside from always converting both involved in the comparison to utc time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T05:30:13+00:00Added an answer on May 24, 2026 at 5:30 am

    When you call .Equal or .Compare, internally the value .InternalTicks is compared, which is a ulong without its first two bits. This field is unequal, because it has been adjusted a couple of hours to represent the time in the universal time: when you call ToUniversalTime(), it adjusts the time with an offset of the current system’s local timezone settings.

    You should see it this way: the DateTime object represents a time in an unnamed timezone, but not a universal time plus timezone. The timezone is either Local (the timezone of your system) or UTC. You might consider this a lack of the DateTime class, but historically it has been implemented as "number of ticks since 1970" and doesn’t contain timezone info.

    When converting to another timezone, the time is — and should be — adjusted. This is probably why Microsoft chose to use a method as opposed to a property, to emphasize that an action is taken when converting to UTC.

    Originally I wrote here that the structs are compared and the flag for System.DateTime.Kind is different. This is not true: it is the amount of ticks that differs:

    t1.Ticks == t2.Ticks;       // false
    t1.Ticks.Equals(t2.Ticks);  // false
    

    To safely compare two dates, you could convert them to the same kind. If you convert any date to universal time before comparing you’ll get the results you’re after:

    DateTime t1 = DateTime.Now;
    DateTime t2 = someOtherTime;
    DateTime.Compare(t1.ToUniversalTime(), t2.ToUniversalTime());  // 0
    DateTime.Equals(t1.ToUniversalTime(), t2.ToUniversalTime());  // true
    

    Converting to UTC time without changing the local time

    Instead of converting to UTC (and in the process leaving the time the same, but the number of ticks different), you can also overwrite the DateTimeKind and set it to UTC (which changes the time, because it is now in UTC, but it compares as equal, as the number of ticks is equal).

    var t1 = DateTime.Now
    var t2 = DateTime.SpecifyKind(t1, DateTimeKind.Utc)
    var areEqual = t1 == t2   // true
    var stillEqual = t1.Equals(t2) // true
    

    I guess that DateTime is one of those rare types that can be bitwise unequal, but compare as equal, or can be bitwise equal (the time part) and compare unequal.

    Changes in .NET 6

    In .NET 6.0, we now have TimeOnly and DateOnly. You can use these to store "just the time of day", of "just the date of the year". Combine these in a struct and you’ll have a Date & Time struct without the historical nuisances of the original DateTime.

    Alternatives

    Working properly with DateTime, TimeZoneInfo, leap seconds, calendars, shifting timezones, durations etc is hard in .NET. I personally prefer NodaTime by Jon Skeet, which gives control back to the programmer in a meaningful an unambiguous way.

    Often, when you’re not interested in the timezones per se, but just the offsets, you can get by with DateTimeOffset.

    This insightful post by Jon Skeet explains in great depth the troubles a programmer can face when trying to circumvent all DateTime issues when just storing everything in UTC.

    Background info from the source

    If you check the DateTime struct in the .NET source, you’ll find a note that explains how originally (in .NET 1.0) the DateTime was just the number of ticks, but that later they added the ability to store whether it was Universal or Local time. If you serialize, however, this info is lost.

    This is the note in the source:

        // This value type represents a date and time.  Every DateTime
        // object has a private field (Ticks) of type Int64 that stores the
        // date and time as the number of 100 nanosecond intervals since
        // 12:00 AM January 1, year 1 A.D. in the proleptic Gregorian Calendar.
        //
        // Starting from V2.0, DateTime also stored some context about its time
        // zone in the form of a 3-state value representing Unspecified, Utc or
        // Local. This is stored in the two top bits of the 64-bit numeric value
        // with the remainder of the bits storing the tick count. This information
        // is only used during time zone conversions and is not part of the
        // identity of the DateTime. Thus, operations like Compare and Equals
        // ignore this state. This is to stay compatible with earlier behavior
        // and performance characteristics and to avoid forcing  people into dealing
        // with the effects of daylight savings. Note, that this has little effect
        // on how the DateTime works except in a context where its specific time
        // zone is needed, such as during conversions and some parsing and formatting
        // cases.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just implemented uploadify in my project, and I noticed what seems like an
I've used DTCTester before to diagnose MSDTC problems. However, I just noticed DTCPing seems
Just noticed that the style of the navigation menu on windows.com is just what
Just noticed this on OSX and I found it curious as I expected long
Just noticed in ByteArrayOutputStream , the toByteArray() is declared as, public synchronized byte toByteArray()[];
I just noticed that the return list for results is limited to 1000. I
I just noticed that Chromium was installed in AppData in both Vista and XP.
I just noticed that you can do this in C#: Unit myUnit = 5;
I just noticed that you can not use standard math operators on an enum
I just noticed while creating a RESTful WCF service that the Method parameter on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.