更新
我正在处理一个遗留数据库,其中日期时间值存储在特定时区(而非 UTC)中。假设无法更改我们存储这些值的方式。
结束更新
假设我有一个带有如下表的 SQL Server 2005 数据库:
[id] (int) not null
[create_date] (datetime) not null
假设我的 [create_date] 按照惯例存储为时区 TZ-A。
假设我想从数据库中检索这个值(使用 SqlClient)并在另一个时区 TZ-B 中显示它。
我该怎么做?
DateTime from_db = // retrieve datetime from database, in timezone TZ-A
DateTime to_display = //convert from_db to another timezone, TZ-B
最佳答案
TimeZoneInfo timeZone1 = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
TimeZoneInfo timeZone2 = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime to_display= TimeZoneInfo.ConvertTime(from_db, timeZone1, timeZone2);
我也同意以 UTC 存储是可行的方法。唯一的缺点是试图向想要自己编写报告的用户解释 UTC。
关于c# - 在 C# 中,将 Sql Server 2005 日期时间值转换为另一个时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2276852/