我该如何重新present时间在.NET中唯一的价值?我该、价值、时间、present

2023-09-02 21:09:53 作者:把心掏给你玩

时有其中一个可以重新present时间在.NET中唯一的价值没有日期的方式?例如,表示店铺的开业时间?

Is there is way in which one can represent a time only value in .NET without the date? For example, indicating the opening time of a shop?

时间跨度表示范围,而我只想要存储的时间值。使用日期时间,以指示这将导致新的DateTime(1,1,1,8,30,0),这是不很令人满意。

TimeSpan indicates a range, whereas I only want to store a time value. Using DateTime to indicate this would result in new DateTime(1,1,1,8,30,0) which is not really desirable.

推荐答案

正如其他人所说,你的可以的使用的DateTime 而忽略日期,或者使用时间跨度。我个人并不热衷于其中任何一个解决方案,因为无论类型真实地反映你要重新present的概念 - 我把日期/时间类型的.NET作为有些对稀疏侧是一原因我开始野田佳彦时间。在野田佳彦时,您可以使用本地时间键入重新present一天的时间。

As others have said, you can use a DateTime and ignore the date, or use a TimeSpan. Personally I'm not keen on either of these solutions, as neither type really reflects the concept you're trying to represent - I regard the date/time types in .NET as somewhat on the sparse side which is one of the reasons I started Noda Time. In Noda Time, you can use the LocalTime type to represent a time of day.

有一点需要注意:一天的时间是不一定的时间长度,因为午夜在同一天...

One thing to consider: the time of day is not necessarily the length of time since midnight on the same day...

(另一个顺便说一句,如果你也想重​​新present一个的关闭的时间一家店,你会发现你要重新present 24:00,即时间在一天结束大多数日期/时间的API - 包括野田时间 - 不允许psented随着时间的日一个值,该值被重新$ P $)

(As another aside, if you're also wanting to represent a closing time of a shop, you may find that you want to represent 24:00, i.e. the time at the end of the day. Most date/time APIs - including Noda Time - don't allow that to be represented as a time-of-day value.)