Michal Rogozinski
Michal Rogozinski

Reputation: 1759

ASP.NET app and local timezone

I have an ASP.NET application that is hosted in timezone A and is being used by users in timezone B. Is there any way to set the whole web application's timezone to B even though the hosting environment is in A? I've already set the globalization tag in web.config with appropriate uiCulture and culture values and they work great for days of the week and other localization settings, unfortunately they did not adjust the clock :/ What is the easiest thing I can do to achieve the adjustment so when in the application i do DataTime.Now, I get the current time in timezone B not A. Thx.

Upvotes: 3

Views: 8708

Answers (3)

Cyril Gupta
Cyril Gupta

Reputation: 13723

I recently had to account for the viewer's timezone in a website I was making. So I first got the server's UTC time and then added the timezone of the viewer (got it through client side scripting).

Then in my code I used that as an offset.

Agreed it's a god-awful way to program and not at all easy or efficient, but that seems like the only way to me.

Upvotes: 0

Peter Stuer
Peter Stuer

Reputation: 1976

Get the client timezone offset from UTC in minutes via a JavaScript Date.getTimezoneOffset() call and pass this back to the server via a hidden input field.

Serverside use DateTime.UTCNow to get the server time in UTC.

Serverside use DateTime.SpecifyKind to convert between server local time amd UTC as needed.

Upvotes: 3

JB King
JB King

Reputation: 11910

Assuming you have permission to make this change: Set the machine's time zone to be B would be my suggestion assuming that all the users of the application are sharing the same time zone.

Otherwise: Store the offset for timezone B from Utc and use that combination though this may run into issues around daylight savings time possibly.

Upvotes: 3

Related Questions