views:

63

answers:

3

I have following scenario:

USA: database & application server

Europe: client

The server reads a date time (e.g. 12:00) object from the database and send it to a client in Europe. The problem is now, the client displays this date time in the time zone of the client (e.g. 18:00), but we need the time in the database, independent of the time zone of the server. On the client we don't know from which time zone this value is.

So how can we achieve this?

+1  A: 

your tags tell the answer.

use the TimeZone Class.

http://msdn.microsoft.com/en-us/library/system.timezone.touniversaltime.aspx

also: http://stackoverflow.com/questions/246498/creating-a-datetime-in-a-specific-time-zone-in-c-fx-3-5

So in your DB, times should be UTC. from there you can do anything what you want.

Stefanvds
TimeZone is obsolete - TimeZoneInfo is your friend.
Jon Skeet
well i linked to your post :)
Stefanvds
+1  A: 

Can't you simply use DateTime.ToUniversalTime()?

http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx

Alternatively, if you don't want UTC, you can find out the timezone of your server and do something like:

DateTime dt;
TimeZoneInfo timezone_EST =
    TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");    
DateTime dt_EST = TimeZoneInfo.ConvertTime(dt, timezone_EST);
Dan Dumitru
The problem of this approach is, that i would have to iterate thru a dataset table with 10k rows to do this.
Enyra
A: 

If you're storing the DateTime data in SQL 2008, take a look at new datetimeoffset type which will store timezone information as well as the date and time themselves

Dav Evans
It doesn't store time zone information. It stores the offset from UTC at that moment in time. Those are *very* different things. Given a UTC instant and a time zone, I would know what the local time would be five minutes later. Given a UTC instant and an offset, I wouldn't.
Jon Skeet
unfortunatelly it is SQL 2005 which does not support this yet.
Enyra