如何从数据库中获取以UTC存储的数据
我已经花了很长时间找到这个问题的解决方案并敲打着我的头,但我非常困惑和绝望。实际上,我使用 DateTime.Now
在服务器端存储出勤时间当用户从网络或电话登录时,我将时间存储在服务器端,如 attendanceData.DateNTime = DateTime.Now ;
采用 UTC 时间。每当网络上的用户想要查看他/她的数据时,我都会使用以下代码将 UTC 时间转换为他/她的当地时间。
List<Model.Attendance> attendanceRecord = attendanceService.GetByDateAndId(this.LoginEmployeeId, DateTime.Now).OrderBy(x => x.DateNTime).ToList();
for (int i = 0; i < attendanceRecord.Count(); i++)
{
attendanceRecord[i].DateNTime =
WebUtils.TimezoneDateConversion(attendanceRecord[i].DateNTime,
this.UserTimeZone);
}
下面是 TimezoneDateConversion 方法:
public static DateTime TimezoneDateConversion(DateTime date, string timezoneid)
{
TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById(timezoneid);
//var dateTimeUTC = DateTime.SpecifyKind(date, DateTimeKind.Local);
var dateTimeSetKind = DateTime.SpecifyKind(date, DateTimeKind.Utc);
return TimeZoneInfo.ConvertTimeFromUtc(dateTimeSetKind, infotime);
}
让我们举个例子,我的本地时间是 1:40 AM 3/4/2022 巴基斯坦标准时间
,服务器时间是“8:40 PM 3/3/2022”。现在,如果我在网上获取数据,它会返回昨天的数据。因为UTC时间晚了5个小时。我知道我在查询中使用 DateTime.Now ,这将是 UTC 时间并获取前一天的数据,然后我将所有时间转换为用户时区。我尝试首先将服务器 DateTime.Now
转换为用户本地时间,然后将其传递给 GetByDateAndId
但它给出了错误的数据。 从数据库获取数据时我可能做错了什么。
这是我的服务器数据库: 现在,当我在 LINQ 中使用
DateTime.UTCnow
获取数据时,它会显示昨天的数据,但我想要根据本地时间获取数据。
It's been a long time I am finding a solution to this problem and banging my head but I am very confused and hopeless. Actually, I am storing attendance time in server-side using DateTime.Now
When user signs in from the web or phone I store the time on server-side like attendanceData.DateNTime = DateTime.Now;
which is in UTC. Whenever users on the web want to see his/her data I am converting the UTC time to his/her local time using the below code.
List<Model.Attendance> attendanceRecord = attendanceService.GetByDateAndId(this.LoginEmployeeId, DateTime.Now).OrderBy(x => x.DateNTime).ToList();
for (int i = 0; i < attendanceRecord.Count(); i++)
{
attendanceRecord[i].DateNTime =
WebUtils.TimezoneDateConversion(attendanceRecord[i].DateNTime,
this.UserTimeZone);
}
Here is TimezoneDateConversion method:
public static DateTime TimezoneDateConversion(DateTime date, string timezoneid)
{
TimeZoneInfo infotime = TimeZoneInfo.FindSystemTimeZoneById(timezoneid);
//var dateTimeUTC = DateTime.SpecifyKind(date, DateTimeKind.Local);
var dateTimeSetKind = DateTime.SpecifyKind(date, DateTimeKind.Utc);
return TimeZoneInfo.ConvertTimeFromUtc(dateTimeSetKind, infotime);
}
Let's have an example my local time is 1:40 AM 3/4/2022 Pakistan Standard Time
and server time is '8:40 PM 3/3/2022'. Now if I get my data on the web it will return me yesterday's data. Because UTC time is 5 hours behind. I know that I am using DateTime.Now in the query which will be UTC time and get data from the previous day and then I am converting all time to user timezone. I tried first converting server DateTime.Now
to user local time then passing it to GetByDateAndId
but it gives the wrong data.
I might be doing something wrong when fetching data from database.
Here is my server database:
Now when I fetch data using DateTime.UTCnow
in LINQ it's showing me yesterday's data but I want data according to my local time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DateTime.Now
将返回服务器的本地时间。来自文档:DateTime.UtcNow
将返回 UTC 时间的计算机时间,无论时区如何。来自文档:
要解决此问题,
请将以下内容更改为:
如果您无法更改该行,则可以采取的另一种选择是在转换日期之前调用
ToUniversalTime()
,例如:但是,这这不是一个很好的解决方案,因为如果您更改服务器时区,转换将不正确。更好的解决方案是首先存储 UTC 时间。
DateTime.Now
will return the local time of your server. From the docs:DateTime.UtcNow
will return the computers time as UTC time regardless of the timezone. From the docs:To solve the problem change this:
to this:
If you can't change that line, another option you can take is to call
ToUniversalTime()
on the date before converting it, for example:However, this is not a great solution because if you ever change the servers timezone that conversion will be incorrect. The better solution is storing UTC times in the first place.