C# 从 DateTime 中提取日期部分
代码行 DateTime d = DateTime.Today;
结果为 10/12/2011 12:00:00 AM
。我怎样才能只获取日期部分。当我比较两个日期时,我需要忽略时间部分。
The line of code DateTime d = DateTime.Today;
results in 10/12/2011 12:00:00 AM
. How can I get only the date part.I need to ignore the time part when I compare two dates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DateTime
是一种数据类型,用于存储Date
和Time
。但它提供了属性来获取Date
部分。您可以从
Date
属性获取日期部分。http://msdn.microsoft.com/en-us/library /system.datetime.date.aspx
DateTime
is a DataType which is used to store bothDate
andTime
. But it provides Properties to get theDate
Part.You can get the Date part from
Date
Property.http://msdn.microsoft.com/en-us/library/system.datetime.date.aspx
没有办法“丢弃”时间部分。
DateTime.Today
与以下内容相同:如果您只想显示日期部分,只需执行此操作 - 将
ToString
与格式字符串一起使用你需要。例如,使用标准格式字符串“D”(长日期格式说明符):
There is no way to "discard" the time component.
DateTime.Today
is the same as:If you only want to display only the date portion, simply do that - use
ToString
with the format string you need.For example, using the standard format string "D" (long date format specifier):
仅比较数据时间的日期时,请使用 Date 属性。所以这应该适合你
When comparing only the date of the datatimes, use the Date property. So this should work fine for you
如果您想比较日期,忽略时间部分,请使用
DateTime.Year
和DateTime.DayOfYear
属性。代码片段
if you want to compare dates, ignoring the time part, make an use of
DateTime.Year
andDateTime.DayOfYear
properties.code snippet
你可以使用格式字符串
you can use a formatstring