Format DateTime List to show only date and no time

时间:2015-07-28 16:01:11

标签: c# asp.net-mvc

I have a list method that gets the next 10 days in a list. The output of this list below is in the DateTime format of e.g. "28/07/2015 00:00:00"

List<DateTime> DateIntervals;
DateIntervals = GetTimeIntervals(
    DateTime.Today.AddDays(0),
    DateTime.Today.AddDays(10), 
    new TimeSpan(24, 0, 0));

How could I achieve the format "28/07/2015" with no time on the end? Would I need to convert it to a list string? What about if I wanted the time only e.g. hour and minute "17:15"?

Somthing like DateIntervals.ToString("dd/MM/yyyy"); will not work for this list

1 个答案:

答案 0 :(得分:5)

DateTime doesn't have a format. You apply a format when converting it to a string for displaying.

You could use Linq to project the list to a list of strings:

var strings = DateIntervals.Select(d => d.ToString("dd/MM/yyyy"));

But this is typically done in the display layer rather than the model.