.NET Date without Time - 有没有,或者我不需要吗?
我正在做一些域建模,并遇到一个属性,在我看来,最好将其公开为日期而不是日期时间,没有时间组件。
框架中没有这种类型是否有充分的理由?向 SQL Server 添加日期类型被认为是一个足够好的主意。另外,如果有人知道 Date 类的方便实现,请告诉我!
2018 年编辑:我现在使用值对象类型来解决这个问题:
public class Date : ValueOf<DateTime, Date> //Install-Package ValueOf
{
protected override void Validate()
{
if (Value.Date != Value) throw new ArgumentException();
}
}
...
var date = Date.From(someDateTime);
I'm doing some domain modelling and coming across a property which to my mind would be best exposed as a Date rather than a DateTime, without a time component.
Is there a good reason why there is no such type in the Framework? It was deemed a good enough idea to add a Date type to SQL Server. Also, if someone knows of a handy implementation of a Date class, please let me know!
2018 edit: I now tackle this using a Value Object Type:
public class Date : ValueOf<DateTime, Date> //Install-Package ValueOf
{
protected override void Validate()
{
if (Value.Date != Value) throw new ArgumentException();
}
}
...
var date = Date.From(someDateTime);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
提问时还没有这样的人,是的,应该有一个。幸运的是,从 .NET 6 开始,我们有了
DateOnly
和TimeOnly
。我不相信除此之外还有什么好的理由 在我(非常有偏见的)看来,.NET 中的日期和时间 API 不是很好。
我是主要维护者的 Noda Time 项目有
LocalDate
和LocalTime
,作为相当大的类型集的一部分。There wasn't one at the time the question was asked, and yes, there should be one. Fortunately, as of .NET 6 we have
DateOnly
andTimeOnly
.I don't believe there was a good reason other than that the date and time API in .NET isn't very good in my (very biased) view.
The Noda Time project of which I'm the primary maintainer has
LocalDate
andLocalTime
, as part of a rather larger set of types.当您需要一个简单的日期而不用担心时间部分、时区时,我创建了一个简单的 Date struct 、本地与 utc 等
https://github.com/claycephas/csharp-date
I created a simple Date struct for times when you need a simple date without worrying about time portion, timezones, local vs. utc, etc.
https://github.com/claycephas/csharp-date
在数据库中,存储容量更为重要,因此使用比日期时间更少空间的日期类型可能更有意义。在应用程序中,您很少需要保存这几个字节。
您可以轻松地创建一个在
DateTime
之间进行转换的Date
类型,但仅在内部存储日期。像这样的东西:In a database storage capacity is more important, so a
date
type that uses less space than adatetime
may make sense. In an application you rarely have the need for saving those few bytes.You could rather easily make a
Date
type that converts to and fromDateTime
, but only stores the days internally. Something like:使用 ToShortDateString() 方法。如果您需要网格或其他东西的属性,请创建一个仅返回 yourDate.ToShortDateString() 的只读属性,您就得到了它!
Use ToShortDateString() method. If you need a property for a grid or something, make a readonly property which just returns yourDate.ToShortDateString() and you got it!
VB.NET 有这种类型,即
Date
数据类型。我相信这是作为祖父 VB6 应用程序代码的要求,其中包含此类型而不是我们在 .NET 中使用的DateTime
类型。日期数据类型 (Visual Basic):
http://msdn.microsoft.com/en-我们/library/3eaydw6e(v=vs.110).aspx
VB.NET has this type, the
Date
data type. I believe this was here as a requirement for grandfathering VB6 app code which contained this type and not theDateTime
type we use in .NET.Date Data Type (Visual Basic):
http://msdn.microsoft.com/en-us/library/3eaydw6e(v=vs.110).aspx
是的,有一个。
Date
、DateTime
(VB) 和DateTime
(C#) 都有一个名为Date
的函数,仅返回日期/时间的日期部分。还有一个
Time
函数,因此您可以将它们用于两者并分别返回日期/时间部分。Yes, there is one.
Date
,DateTime
(VB) andDateTime
(C#) both have a function calledDate
on them that return only the date portion of the date/time.There is also a
Time
function so you can use them for both and return the date/time parts separately.实际上没有任何必要 - 只需使用时间部分设置为零(即前一天的午夜)的日期,或者完全忽略时间部分。
例如,您可以使用
DateTime.Today
获取今天的日期(而不是DateTime.Now
)There isn't really any need for one - just use a date with the time component set to zero (i.e. midnight of the previous day), or just ignore the time component completely.
For example you can use
DateTime.Today
to get today's date (as opposed toDateTime.Now
)