开发者

Read a DateTime object in a different time zone

开发者 https://www.devze.com 2023-01-16 08:18 出处:网络
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 开发者_Go百科to a client in Europe. T

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 开发者_Go百科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?


your tags tell the answer.

use the TimeZone Class.

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

also: 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.


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);


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

0

精彩评论

暂无评论...
验证码 换一张
取 消