如何从数据库中获取以UTC存储的数据

发布于 2025-01-11 15:36:32 字数 1637 浏览 3 评论 0原文

我已经花了很长时间找到这个问题的解决方案并敲打着我的头,但我非常困惑和绝望。实际上,我使用 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:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场春暖 2025-01-18 15:36:32

当用户从网络或电话登录时,我将时间存储在服务器端,例如 attendanceData.DateNTime = DateTime.Now;

DateTime.Now 将返回服务器的本地时间。来自文档:

获取一个 DateTime 对象,该对象设置为此计算机上的当前日期和时间(以本地时间表示)。

DateTime.UtcNow将返回 UTC 时间的计算机时间,无论时区如何。来自文档:

获取一个 DateTime 对象,该对象设置为此计算机上的当前日期和时间,表示为协调世界时 (UTC)。

要解决此问题,

attendanceData.DateNTime = DateTime.Now;

请将以下内容更改为:

attendanceData.DateNTime = DateTime.UtcNow;

如果您无法更改该行,则可以采取的另一种选择是在转换日期之前调用 ToUniversalTime() ,例如:

attendanceRecord[i].DateNTime = WebUtils.TimezoneDateConversion(attendanceRecord[i].DateNTime.ToUniversalTime(), this.UserTimeZone);

但是,这这不是一个很好的解决方案,因为如果您更改服务器时区,转换将不正确。更好的解决方案是首先存储 UTC 时间。

When user signs in from the web or phone I store the time on server-side like attendanceData.DateNTime = DateTime.Now;

DateTime.Now will return the local time of your server. From the docs:

Gets a DateTime object that is set to the current date and time on this computer, expressed as the local time.

DateTime.UtcNow will return the computers time as UTC time regardless of the timezone. From the docs:

Gets a DateTime object that is set to the current date and time on this computer, expressed as the Coordinated Universal Time (UTC).

To solve the problem change this:

attendanceData.DateNTime = DateTime.Now;

to this:

attendanceData.DateNTime = DateTime.UtcNow;

If you can't change that line, another option you can take is to call ToUniversalTime() on the date before converting it, for example:

attendanceRecord[i].DateNTime = WebUtils.TimezoneDateConversion(attendanceRecord[i].DateNTime.ToUniversalTime(), this.UserTimeZone);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文