如何在 .NET 中表示仅时间值? [英] How do I represent a time only value in .NET?

查看:33
本文介绍了如何在 .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 并忽略日期,或使用 <代码>时间跨度.就我个人而言,我并不热衷于这些解决方案中的任何一个,因为这两种类型都没有真正反映您试图表示的概念 - 我认为 .NET 中的日期/时间类型在某种程度上是稀疏的,这是我开始的原因之一野田时间.在野田时间中,您可以使用 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 Time - 不允许将其表示为时间值.)

(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天全站免登陆