DateTime and DateTimeOffset

The DateTime object is heavily used in .Net coding, and it includes a “Kind” property which basically indicates whether the object is representing a UTC datetime or one that is localized to the current timezone. But, suppose you want to store a DateTime that also keeps track of a different timezone- for instance, if you wanted to capture the “local time” of a remote user of your software, and it’s important to know that it was 6PM at their particular spot. This is where a traditional DateTime falls short- we cannot specify a specific timezone that is different from our own local one, and any attempt to do so just results in it being translated to our local zone.

What is a timezone anyway? Aside from the names and various daylight savings rules etc, a timezone is just a simple offset of the hours (and minutes) from the core UTC date.

.Net provides us with another class: The DateTimeOffset. We can use this to store normal Date and Time info, but also includes a TimeSpan property for the amount of offset, so now we can store that additional bit of information with our datetime objects and capture the remote users’ timezone offset info with each date.

The nice thing about DateTimeOffsets is you can still easily convert them back into normal DateTime’s- either to a localized date or a UTC one. Simply utilize the .DateTime or UtcDateTime propertys to access the converted DateTime objects.