Skip to content

Notes

Handling Date and Time with Timezone awareness correctly in .NET

The DateTime type isn't enough for an application that needs to work with different timezones. You want a consistent way of handling date/time on the server and on the client.

Posted on Tuesday, January 15, 2019

Problem statement

The TimezoneInfo class and most important methods

  • ConvertTimeToUtc(DateTime dateTime, TimeZoneInfo sourceTimeZone)
  • DateTime ConvertTimeFromUtc( DateTime dateTime, TimeZoneInfo destinationTimeZone)
  • Fallacy of DateTime.ToUniversalTime(): GIST

Using DateTimeOffset to encapsulate both the date/time and the UTC offset of the timezone in question

Write tests to ensure your conversions of date time from and to UTC work as expected, and run the tests with host set to different time zones. (a host running in Sydney timezone vs a host running in UTC)