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
5
  • 18
    DateTimeOffset doesn't fix the DST problem Commented Jan 15, 2013 at 18:47
  • 4
    Using TimeZoneInfo class does carry rules for DST. if you are on .net 3.5 or later then use either TimeZone or TimeZoneInfo classes to deal with dates that must handle Daylight Savings Time in conjunciton with the timezone offset. Commented Mar 29, 2013 at 11:41
  • 1
    Yes good example of an exception (the alarm app) but when the time is more important than the date you should really store that separate in your schedule data structure for the application, i.e. occurrence type = Daily and time = 09:00. The point here is the developer needs to be aware of what type of date they are recording, calculating or presenting to users. Especially apps tend to be more global now we have the internet as standard and big app stores to write software for. As a side node I'd also like to see Microsoft add a separate Date and Time structure. Commented Apr 9, 2014 at 9:12
  • 5
    Summarizing Jarrett's and Zack's comments: It sounds like DateTimeOffset alone will not handle the DST problem but using DateTimeOffset in conjunction with TimeZoneInfo will handle it. This is no different from DateTime where kind is Utc. In both cases I must know the time zone (not just the offset) of the calendar I am projecting the moment to. (I might store that in a user's profile or get it from the client (e.g. Windows) if possible). Sound right? Commented Nov 8, 2014 at 5:31
  • 3
    "There's a few places where DateTimeOffset makes sense." --- Arguably, it more often makes sense than not. Commented Dec 5, 2018 at 20:28