DateTimeOffset Type
DateTimeOffset contains the time zone information that
DateTime lacks.
This type is best for representing a specific moment of time in the past or the near future.
Use the
DateTimeOffsetFromParts function to create a
DateTimeOffset value.
For moments of time in the distant future, more care is required because time zones can change.
There are two scenarios to consider:
- Daylight Saving Time rules can change.
For instance, laws have been proposed in the United States to switch to year-round Daylight Saving Time (DST).
- The time zone used by a particular geographical region can change.
For instance, Mercer County in North Dakota changed from Mountain Time to Central Time in 2010.
Consider a
DateTimeOffset for 6:00 PM Eastern time on January 1st, 2050.
With current DST rules, the Eastern time zone at that moment will be 5 hours behind UTC.
However, if the rules change to year-round DST, then the Eastern time zone will instead be 4 hours behind UTC.
A
DateTimeOffset created prior to the rule change, for a date after the DST change goes into effect, will have the incorrect
offset value.
If your application needs to represent distant future dates correctly, the safest way is to store a geographical location combined with a
DateTime representing local time at that location.
For instance, a
DateTime for January 1st, 2050 at 6:00 PM with the location "New York City" is fully robust against time zone changes.
This representation will identify the correct moment in time even if New York City switches to another time zone or changes its DST rules.