我需要删除日期时间的时间部分,或者可能有以下格式的日期在对象形式,而不是在字符串形式。

06/26/2009 00:00:00:000

我不能使用任何字符串转换方法,因为我需要对象形式的日期。

我尝试先将DateTime转换为字符串,从它中删除特定的时间日期,但它添加了12:00:00 AM,只要我将它转换回DateTime对象。


当前回答

你不能。. net中的DateTime总是有一个时间,默认为00:00:00:000。DateTime的Date属性也是DateTime(!),因此时间默认为00:00:00:000。

这是. net框架的不足之处,可以认为。net中的DateTime违反了单一责任原则。

其他回答

DateTime。日期

var newDate = DateTime.Now; //newDate.Date property is date portion of DateTime

这可以简单地这样做:

var dateOnly = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day)

你不能。. net中的DateTime总是有一个时间,默认为00:00:00:000。DateTime的Date属性也是DateTime(!),因此时间默认为00:00:00:000。

这是. net框架的不足之处,可以认为。net中的DateTime违反了单一责任原则。

如果你想使用绑定,只显示日期部分而不显示时间

ToolTip=“{{Binding TransactionModel.TransactionDate, StringFormat=d}”

这种方法只得到日期而没有时间

DateTime date = DateTime.Now;
string Strdateonly = date.ToString("d");

输出= 5/16/2015