c# - 从 C# 中的 T-Sql 数据库读取以 UTC 格式保存的 DateTime

标签 c# tsql datetime utc

我将 t-sql 数据库中的日期时间保存为 UTC 而不是本地时间,即保存后,它丢失了 UTC 日期,例如:2011-11-08 00:00:00.000。在 C# 中从 db 读取时,它被读取为本地时间而不是 UTC。例如:读取日期时间值后,dateTime.ToUniversalTime() 给出不同的值。

如何将数据库日期时间值读取为 UTC 而不是本地时间?或者我应该在本地时间保存在 t-sql 中?

最佳答案

好吧,如果你得到一个 DateTimeKindUnspecified,你可以使用:

DateTime utc = DateTime.SpecifyKind(unspecified, DateTimeKind.Utc);

此类问题只是reasons I dislike DateTime 中的一个问题.

关于c# - 从 C# 中的 T-Sql 数据库读取以 UTC 格式保存的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8096679/

相关文章:

c# - 如何将该运算符 C# 代码表示为 java?

sql-server - sql 从函数或存储过程返回表

java - 将字符串转换为时间,更改时区,然后返回字符串

java - 为什么 Calendar.before() 和 Calendar.after() 接受对象值?

MySQL 日期之间无法正常工作

c# - 在不同的类中实现 INotifyPropertyChanged

c# - 剑道用户界面 : How to get new file name in javascript after renaming uploaded file in controller

c# - 哪个 C# 程序集包含 Invoke?

tsql - 代码复用 tsqlt/sql 测试

sql - 合并两个 select 语句的结果