如何在.NET中代表一个时间唯一的价值? [英] How do I represent a time only value in .NET?

查看:118
本文介绍了如何在.NET中代表一个时间唯一的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法可以代表.NET中的唯一值,而没有日期?例如,指示商店的开放时间?

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

TimeSpan 表示一个范围,而我只想存储一个时间值。使用 DateTime 来表示这将导致新的 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 并忽略日期,或使用 TimeSpan 。就我个人而言,我不喜欢这两种解决方案,因为这两种类型都没有真正反映出你想要表达的概念 - 我认为.NET中的日期/时间类型在稀疏的一面是我开始的原因之一 Noda时间。在Noda时间,您可以使用 LocalTime 类型来表示一天中的时间。

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...

(另外,如果你还想代表一个关闭商店的时间,您可能会发现您要代表24:00,即一天结束的时间。大多数日期/时间API(包括Noda时间)不允许被表示为日期值。)

(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.)

这篇关于如何在.NET中代表一个时间唯一的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆