Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

DateTime vs DateTimeOffset

What is the difference between a DateTime and a DateTimeOffset and when should one be used?


Currently, we have a standard way of dealing with .NET DateTimes in a TimeZone-aware way: Whenever we produce a DateTime we do it in UTC (e.g. using DateTime.UtcNow), and whenever we display one, we convert back from UTC to the user's local time.

That works fine, but I've been reading about DateTimeOffset and how it captures the local and UTC time in the object itself.

Answer*

Cancel
4
  • 31
    with all the above answers, I wonder why no one bothered to write your single sentence that sums it all up The most important distinction is that DateTime does not store time zone information, while DateTimeOffset does. Commented May 15, 2016 at 22:38
  • 18
    DateTimeOffset does NOT store time zone info. MS doc titled "Choosing between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo" specifies this stating: "A DateTimeOffset value is not tied to a particular time zone, but can originate from any of a variety of time zones". That said, DateTimeOffset IS time zone AWARE, containing the offset from UTC, which makes all the difference and is why it's MS recommended default class when dealing with app development that deals with date info. If you truly care about which specific timezone the data came from, you must preserve that separately Commented Jun 14, 2017 at 20:35
  • 2
    Yes, but as has been shown in many places, + or - hours says nothing about what timezone you were in and is ultimately useless. Depending on what you need to do, you can just as well store a datetime as Kind.Unspecified and then store the id of its timezone and I think you are actually better off. Commented Apr 6, 2018 at 13:14
  • 1
    It's true that an offset isn't the same thing as time zone, but it's far simpler to understand and is usually what is important, far from useless. It is true that a timestamp with an offset of -5 could have happened in Eastern Standard Time or in Central Daylight Time. But if I don't care what time zone it was in, if I just want to know when the event happened (which is what a timestamp is usually for), the offset is more useful, because I don't need to consult a historical, time zone database in order to convert it to UTC. Commented May 16, 2023 at 17:07