我这样用过,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString(), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
我也尝试过这样,
DateTime dueDate;
DateTime.TryParse(dataRead["Date required"].ToString("dd/MM/yyyy"), out dueDate);
list.add(new list { DueDate = dueDate.ToShortDateString() });
但没有反映出来。我还将访问数据库格式更改为短日期,但这也没有给出正确答案。
答案 0 :(得分:2)
您可以使用dueDate.Date
获取此实例的日期组件。
示例强>
DateTime date1 = new DateTime(2008, 6, 1, 7, 47, 0);
Console.WriteLine(date1.ToString());
// Get date-only portion of date, without its time.
DateTime dateOnly = date1.Date;
// Display date using short date string.
Console.WriteLine(dateOnly.ToString("d"));
// Display date using 24-hour clock.
Console.WriteLine(dateOnly.ToString("g"));
Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));
<强>结果
// The example displays output like the following output:
// 6/1/2008 7:47:00 AM
// 6/1/2008
// 6/1/2008 12:00 AM
//
答案 1 :(得分:0)
试试这个
DateTime dueDateTime = Convert.ToDateTime(row["Date required"].ToString());
DateTime dueDate= dueDateTime.Date;
首先将其转换为Stting,然后转换为DateTime 然后从 DateTime
获取日期