50

What is difference between a DateTime and a DateTimeOffset object?

And when should we use each one?

In a web-application that may change the server's area, storing date and time. Which one is better, or is there any other suggestions?

leppie
  • 115,091
  • 17
  • 196
  • 297
amiry jd
  • 27,021
  • 30
  • 116
  • 215

2 Answers2

31

DateTimeOffset Represents a point in time, typically expressed as a date and time of day, relative to Coordinated Universal Time (UTC) it provides a greater degree of time zone awareness than the DateTime structure. See it here- http://msdn.microsoft.com/en-us/library/bb546101.aspx.

DotNetUser
  • 6,494
  • 1
  • 25
  • 27
  • 15
    so thanks. helpful :D I cant understand why some peaple vote down on my simple questions!!! I'm new and I should learn more and more, and also I should ask my probelems! – amiry jd Jan 31 '12 at 01:33
  • 1
    The only degree of "time zone awareness" it has is it stores the UTC offset for a particular instant in time. It has no other knowledge of time zones, including the time zone the value was created from, so its only real use is with historical data. It is pretty useless for anything else. – Suncat2000 Oct 09 '17 at 11:32
  • 1
    @Suncat2000, and this offset makes all the difference! (it brings 'time zone awareness'). When you take photos your camera assigns local time to it (say 'DateTime'). If it would save equivalent to 'DateTimeOffset' you wouldn't have problem with answer 'do I made this photo at 9:00am or 10:00am' while travelling or across daylight saving changes. One wrong architectural decision made a problem for all photographers who care about time. The same applies to all DateTime fields in IT systems. – Andrzej Martyna Apr 10 '18 at 10:24
  • @AndrzejMartyna That's not time zone awareness. It's just an offset from UTC that tells you next to nothing. You can't reliably determine anything about time zones from it except the datetime value came from a time zone when it was created. Worthless. Either keep strictly to UTC and avoid the confusion or store a time zone identifier with it so you can perform meaningful conversions. DATETIMEOFFSET is worthless. – Suncat2000 Apr 16 '18 at 19:40
  • @Suncat2000, partly agree. I think it is all about developers' adherence to rules use set in the project. If you use DateTime you are dependent of a developer's current mood. It is a little harder to fool DateTimeOffset. The solution can be to implement your class and stop using DateTime completely (as you suggested to use two fields always - BTW. do you see such approach often? ;). – Andrzej Martyna Apr 17 '18 at 10:07
22

DateTimeOffset Overcomes the drawback of DateTime. It expressed as a date and time of day, relative to Coordinated Universal Time (UTC). For Example:

Given 4/18/2013 11:00:00 AM means absolutely nothing if you don't have a reference point. That could be 11:00:00 AM anywhere in the world. DateTimeOffset contains information about the timezone you are dealing with, which makes all the difference in THE WORLD!

To more details must read once

0xced
  • 25,219
  • 10
  • 103
  • 255
Ajay Sharma
  • 2,881
  • 5
  • 22
  • 32
  • 1
    The only difference is that it stores _only_ the UTC offset for the particular instant in time that a DateTime represents. It _does not_ store the actual time zone, so basically all you can do with it is convert between its relative local time and UTC. Helpful in some circumstances, but pretty weak information, otherwise. What DateTimeOffset is not is a time-zone-aware DateTime class. – Suncat2000 Oct 09 '17 at 11:37