3

For as long as I've been using .NET, I have been storing date and time data in the .NET & SQL Server DateTime data types. I recently became aware of the existence of the DateTimeOffset types in both .NET & SQL Server.

Should the DateTimeOffset type now become my default data type for working with and storing dates with time?

If not, when would I use one over the other?

John Mills
  • 10,020
  • 12
  • 74
  • 121

1 Answers1

5

DateTime

Well in my case i use Datetime as it is easier and I have been using it ever since I started programming.

  • Work with dates only
  • Work with times only
  • Work with abstract dates and times
  • Work with dates and times for which time zone information is missing
  • Work with UTC dates and times only
  • Retrieve date and time information from sources outside of .NET, such as SQL databases. Typically, these sources store date and time information in a simple format that is compatible with the DateTime structure

  • Perform date and time arithmetic, but are concerned with general results. For example, in an addition operation that adds six months to a particular date and time, it is often not important whether the result is adjusted for daylight saving time

DateTimeOffest

The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC. Thus, the value always unambiguously identifies a single point in time.

The DateTimeOffset type includes all of the functionality of the DateTime type along with time zone awareness. This makes it suitable for applications that do the following:

  • Uniquely and unambiguously identify a single point in time. The DateTimeOffset type can be used to unambiguously define the meaning of "now", to log transaction times, to log the times of system or application events, and to record file creation and modification times

  • Perform general date and time arithmetic

  • Preserve multiple related times, as long as those times are stored as two separate values or as two members of a structure

Well use whichever suits your needs. In my case I've been using DateTime and it hasn't given me an issue at all but yeah if you like go for the OffsetDateTime.

JEllery
  • 304
  • 2
  • 13