如何存储我的日期?

发布于 2025-01-03 17:48:29 字数 482 浏览 6 评论 0原文

我对 Utc Dates 没有太多经验,但据我所知,它们在大多数情况下是存储日期的最佳方式。

但我目前正在开发一个程序,我不确定存储日期的最佳方式是什么。在该计划中,用户可以从注册之日开始学习五周的课程。每天他都必须填写一张表格,并为当天保存。

目前,我正在将课程的 StartDateEndDate 保存为 DateTime(无 Utc),我应该将其保存为 Utc,还是“有必要吗?因为我只需要这一天(如果用户在 2 月 8 日 10:04 注册,系统需要知道的是 2 月 8 日是课程的第一天。是否有更好的方法来存储这些信息?以及什么这是保存系统事件日期(例如用户登录、帐户创建等)的最佳方法吗?

我还用 DateTime 来存储表单数据,以计算它是哪一天已经提交了,但是我改了所以它只保存相对日期(例如第 5 天)。

我使用的是 C# 和 SQL Server 数据库。

I don't have much experience with Utc Dates, but from what I know, they are in most cases the best way to store dates.

But I'm currently working on a program and I'm in doubt what will be the best way to store my dates. In the program, a user can follow a course for five weeks, starting on the day of registration. Every day he has to fill in a form, which is saved for that day.

Currently, I'm saving the StartDate and EndDate of the course as DateTime (no Utc) Should I save this as Utc, or isn't it necessary? Because I only need the day (if the user registers on February 8th at 10:04, all the system needs to know is that February 8th is the first day of the course. Is there maybe a better way to store this information? And what would be the best way to save system-event-dates (like user logged in, account created, etc)?

I used to store the form-data also with a DateTime, to calculate on which day it was submitted, but I changed it so it only saves the relative day-number (e.g. day 5).

Btw, I'm using C# and a SQL Server database.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

风筝有风,海豚有海 2025-01-10 17:48:29

UTC(协调世界时)与日期的时间部分有关。这是存储日期时间的标准方法,以便您可以进行比较而不必担心时区。

如果您担心的只是日期组件和日期组件。时区/位置并不重要,您可以将其存储在数据库中为 2012-02-01 00:00:00

您可以使用 Alternative 在 .NET 中获取仅日期组件,

System.DateTime.Today;

如果您正在使用某些其他日期...

DateTime dt = DateTime.Now;
dt.Date; // <--- this will give the Date Component with the Time stripped back to 00:00:00

UTC (Coordinated Universal Time) has to do with the time component of a date. It's a standard way to store DateTimes so that you can compare without worrying about timezones.

If all you are worried about is the Date Component & Timezone/Location is unimportant you could just store it in the database as 2012-02-01 00:00:00

You can get the date only component in .NET using

System.DateTime.Today;

Alternatively if you're working with some other Date...

DateTime dt = DateTime.Now;
dt.Date; // <--- this will give the Date Component with the Time stripped back to 00:00:00
时光瘦了 2025-01-10 17:48:29

为什么不将日期存储在 DATE 数据库列中,而不用担心时区?

Why don't you just store the dates in a DATE database column, and not worry about time zones at all?

寄风 2025-01-10 17:48:29

通常,如果您的系统存在能够与不同时区的用户一起工作的功能要求,您希望以 UTC 进行存储。

假设您有一位位于香港的用户和一位位于悉尼的用户正在查看同一事件。如果您希望他们都看到各自时区的活动日期(和时间),那么您可能需要将其存储在 UTC 中,然后根据用户的地理位置呈现给用户。

如果您没有这样的要求,则不会进行此类转换,并且只假设一个时区,那么您不需要向系统添加更多复杂性,只需使用“日期”列并将当前日期存储在那里即可。

但如果你这样做,那就选择 UTC。在这种情况下,您将需要 DateTime,而不仅仅是 Date。这是因为一个时区的 21:30 可能是另一个时区的第二天 2:30,所以时间确实很重要。

当向用户显示时,您可以将其转换为用户的时区,然后丢弃时间,但为了使转换正确,您需要保留时间。

在 .NET 中使用 UTC 很容易,DateTime 有 .ToUniversalTime() 方法,可以为您将日期时间值转换为 UTC:

var utcNow = DateTime.Now.ToUniversalTime();

还有一个属性:

var utcNow = DateTime.UtcNow;

EDITED

使用 TimeZoneInfo 静态类将日期时间从/ 到您需要的时区(例如从 UTC 到指定用户的时区:

TimeZoneInfo.ConvertTimeToUtc(...)
TimeZoneInfo.ConvertTimeFromUtc(...)

Generally you want to store in UTC if there is a functional requirement for your system to be able to work with users in different timezones.

Let's say you have a user in Hong Kong and a user in Sydney looking at the same event. If you want them both see the event date (and time) in their timezones, then here we are, you will probably need to store it in UTC and then present to users respecting their geo locations.

If you don't have such requirements, you don't do such conversions and you only assume one timezone then you don't need to add more complexity into your system, just use Date column and store the current date there.

But if you do - go for UTC. In this scenario you will need DateTime, not just Date. This is because 21:30 in one timezone can be 2:30 next day in another timezone so time really matters.

When showing to a user you may convert it to the user's timezone and then throw time away, but in order to make the conversion correct you will need to keep time.

It is easy to work with UTC in .NET, DateTime has .ToUniversalTime() method that converts a datetime value to UTC for you:

var utcNow = DateTime.Now.ToUniversalTime();

there is also a property:

var utcNow = DateTime.UtcNow;

EDITED

Use TimeZoneInfo static class to convert datetimes from/to timezones you need (for example from UTC to the specified user's timezone:

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