CSharp - DateTime and DateTimeOffset


DateTime and DateTimeOffset are immutable structs for representing a date and time.

They have a resolution of 100 ns, and a range covering the years 0001 through 9999.

DateTimeOffset is similar to Date Time and also stores a UTC offset. It is meaningful when comparing values across different time zones.

DateTime vs DateTimeOffset

DateTime and DateTimeOffset differ in how they handle time zones.

A DateTime incorporates a three-state flag indicating whether the DateTime is relative to:

  • The local time on the current computer
  • UTC (Greenwich Mean Time)
  • Unspecified

A DateTimeOffset is more specific-it stores the offset from UTC as a TimeSpan. For example

July 01 2018 03:00:00 -05:00

DateTime ignores the three-state flag in comparisons and considers two values equal if they have the same year, month, day, hour, minute, and so on.

DateTimeOffset considers two values equal if they refer to the same point in time.

So, DateTime considers the following two values different, whereas DateTimeOffset considers them equal:

July 01 2018 09:00:00 +00:00 (GMT)
July 01 2018 03:00:00 -06:00 (local time, Central America)

In most cases, DateTimeOffset's equality logic is preferable.

DateTime is better at representing a local date and time.