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

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

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

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


当前回答

您可以使用像https://www.nuget.org/packages/Date/这样的Date库 使用Date值,将它们转换为DateTimes,等等。

免责声明我是上述软件包的作者。

其他回答

string dt = myCalender.SelectedDate.ToString();
string date = dt.Remove(10);
displayDate.Content = date;

如果你从日历上取日期,我们也会得到时间。这不是一直都需要的。使用它,我们可以从日期中删除时间。

这可以简单地这样做:

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

在试图解决最初的Q时,偶然发现了这篇文章。

我在用Asp。Net和一些研究之后,我发现当你绑定到代码后面的日期值时,你可以删除时间,这样它就不会显示在屏幕上。

C#:

DateTime Today = DateTime.Now;

aspx:

<%: this.Today.ToShortDateString() %>
DateTime dd=DateTiem.Now;
string date=dd.toString("dd/MM/YYYY");
static void Main(string[] args)
{
    string dateStrings =  "2014-09-01T03:00:00+00:00" ;

    DateTime convertedDate = DateTime.Parse(dateStrings);
    Console.WriteLine("  {0} ----------------- {1}",

    convertedDate,DateTime.Parse(convertedDate.ToString()).ToString("dd/MM/yyyy"));

    Console.Read();
}