views:

1837

answers:

3

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.

+2  A: 

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.

JB King
unfortunately I can't change to whole server's timezone. The only thing I want to change is the timezone for one website in IIS. Ideally to make a setting in web.config... just like culture/localization info.
Michal Rogozinski
No, closest you could do would be to store an offset in the web.config and then extend the DateTime class to use that offset so that a "MyDateTime.Now" is in the desired time zone.
JB King
Unfortunately it doesn't work either, due to Daylight Savings Time which kicks in at different times for Europe and USA :/ I can see that I need to store GMT offset and then adjust each time operation to the particular time zone. Thanks for your answers , I was hoping that there could be some more automated solutions.
Michal Rogozinski
I think the TimeZoneInfo class in .net 3.5 handles adding an offset correctly. Alternatively you might consider this: http://www.crankedup.com/code/
Greg
Thanks I'll try that!
Michal Rogozinski
Hi Again, I've implemented TimeZoneInfo class, works great!!! Thanks for your help.
Michal Rogozinski
+1  A: 

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.

Peter Stuer
Thanks for the suggestion. I'd rather avoid client-server interaction for this. I'm sure I can dig this information out from Request somehow.
Michal Rogozinski
A: 

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.

Cyril Gupta