Few considerations about .NET and Timezones in web scenarios

Probably every web developer has already had topics around time zones. I again had it. I just want to make another notice for the developer community around the globe (and my future me) how I came around this topic now.
In .NET there are two ways to get the time zone (why having only one):

TimeZone.CurrentTimeZone // type System.TimeZone
TimeZoneInfo.Local // type System.TimeZoneInfo

The TimeZoneInfo is the newer one satisfying some features required in Vista or something :). With this class one could also convert times into different time zones using the TimeZoneInfo.Id:

// TimeZoneInfo.Local.Id is something like "W. Europe Standard Time"
TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, TimeZoneInfo.Local.Id);

Altogether this whole topic is not so nice. It is not so easy to reproduce problems when developing against a local web server. I managed to do a “hacky” workaround:

  1. Stop the IIS Express processes
  2. Set local time zone at the taskbar’s clock usually on the bottom right of your desktop – so defining the time zone of the web server
  3. Start the debugging – this starts a IIS Express for you with your chosen time zone
  4. Add watch TimeZoneInfo.Local – so that you see what the current time zone is.
    You will need it, to be always aware what the current server time zone is. It was also written somewhere that this information is not thread and not even process safe.
  5. While running the app, change to your favorite client time zone on your clock, so that your browser’s time zone is according to your problem to reproduce

My specific problem was on Kendo grids where binding to a DateTime struct happens automatically by specifying the DateTimeKind…
I know it is hacky – but it’s working – hope I helped someone.


About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s