仅在C#中的日期类型 - 为什么没有日期类型? [英] A type for Date only in C# - why is there no Date type?
问题描述
在我们的C#项目中,我们需要代表一个日期没有时间。我知道DateTime的存在,但是它也包含了一天的时间。 我想明确指出某些变量和方法参数是基于日期的。因此,我不能使用 DateTime.Date
属性
In our C# project we have the need for representing a date without a time. I know of the existence of the DateTime, however, it incorporates a time of day as well. I want to make explicit that certain variables and method-arguments are date-based. Hence I can't use the DateTime.Date
property
这个问题的标准方法是什么?当然我不是第一个遇到这个吗?为什么C#中没有日期
类?
What are the standard approaches to this problem? Surely I'm not the first to encounter this? Why is there no Date
class in C#?
有没有人有一个很好的实现使用一个结构,也许一些扩展方法在DateTime上,也许实现一些运算符,如==和<>??
Does anyone have a nice implementation using a struct and maybe some extensionmethods on DateTime and maybe implementing some operators such as == and <, > ?
推荐答案
我怀疑没有奉献纯粹的 Date
class,因为您已经有可以处理它的 DateTime
Date
会导致重复和混淆。
I suspect there is no dedicate pure Date
class because you already have DateTime
which can handle it. Having Date
would lead to duplication and confusion.
如果你想要标准的方法看看 DateTime.Date
属性,它只给出一个 DateTime
,时间值设置为午夜12:00(00:00:00)。
If you want the standard approach look at the DateTime.Date
property which gives just the date portion of a DateTime
with the time value set to 12:00:00 midnight (00:00:00).
这篇关于仅在C#中的日期类型 - 为什么没有日期类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!