c# - 在 C# 中,将 Sql Server 2005 日期时间值转换为另一个时区

标签 c# sql-server-2005 datetime timezone

更新

我正在处理一个遗留数据库,其中日期时间值存储在特定时区(而非 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

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/

相关文章:

c# - 使用 C# 将图像上传到 .NET 服务器

c# - 在特定位置禁用 HttpModule

sql-server-2005 - 以字节为单位存储文件大小的最佳方法?

ruby-on-rails - Rails 格式 无 日期

c# - 生成程序网格时出现旋转问题

c# - 如何从 C# 中的 Dictionary<List, List<string>> 创建 .csv 文件

sql-server - MS SQL Server 中的扩展存储过程是什么?

Sql 触发器 - 它属于哪个表?

r - 将时间转换为 "day"和 "night"

python - 在午夜分割数据帧条目