我需要删除日期时间的时间部分,或者可能有以下格式的日期在对象形式,而不是在字符串形式。
06/26/2009 00:00:00:000
我不能使用任何字符串转换方法,因为我需要对象形式的日期。
我尝试先将DateTime转换为字符串,从它中删除特定的时间日期,但它添加了12:00:00 AM,只要我将它转换回DateTime对象。
我需要删除日期时间的时间部分,或者可能有以下格式的日期在对象形式,而不是在字符串形式。
06/26/2009 00:00:00:000
我不能使用任何字符串转换方法,因为我需要对象形式的日期。
我尝试先将DateTime转换为字符串,从它中删除特定的时间日期,但它添加了12:00:00 AM,只要我将它转换回DateTime对象。
当前回答
以上答案都没有解决我的winforms问题。
到达ONLY date的最简单方法是Datetime中的简单函数:
DateTime dt = DateTime.now;
String BirthDate = dt.ToShortDateString();
你将只在生日字符串中有日期。
其他回答
string dt = myCalender.SelectedDate.ToString();
string date = dt.Remove(10);
displayDate.Content = date;
如果你从日历上取日期,我们也会得到时间。这不是一直都需要的。使用它,我们可以从日期中删除时间。
在我的经验中,上述解决方案都不起作用,可能是因为我想从数据库中提取的日期中删除时间,但下面的代码工作得很好:
var date = target_date.Value.ToString("dd/MM/yyyy");
使用date. toshortdatestring()获取没有时间组件的日期
var date = DateTime.Now
var shortDate = date.ToShortDateString() //will give you 16/01/2019
使用date. tostring()自定义日期格式
var date = DateTime.Now
var shortDate = date.ToString('dd-MMM-yyyy') //will give you 16-Jan-2019
这段代码使您可以清楚地分别编写日期和时间
string time = DateTime.Now.Hour.ToString("00") + ":" + DateTime.Now.Minute.ToString("00") + ":" + DateTime.Now.Second.ToString("00");
string date = DateTime.Now.ToString("M-dd-yyyy");
MessageBox.Show(date + "\n" + time);
希望这能有所帮助。
我认为你会这样:DateTime onlyDate = DateTime. today . date; 或者,这是相同的DateTime onlyDate = yourDateTime.Date; 所以使用属性Date。