2

I have an object List called list and I converted it by:

List<DateTime> dateList = list.Select(date => Convert.ToDateTime(date[0])).ToList();

My object list contained values such as 09/04/2015 but when I do this list conversion I get values in the format of 09/04/2015 00:00:00.

I was just wondering how I can get it so just the date is stored in the dateList?

The .Date property does not work

GratefulDemon
  • 87
  • 2
  • 10

1 Answers1

1

.NET doesn't really have a type that represents just a Date. However, if you know how you want your date to be displayed at this point in your code, then you can convert it into a string.

const string dateFormat = "d"; // e.g. "9/4/2015"
List<string> dateList = list
    .Select(date => Convert.ToDateTime(date[0]).ToString(dateFormat))
    .ToList();

You can change the dateFormat to use any of the standard strings listed here, or using a custom format that you can build, as described here.

Here is a fiddle.

Shaun Luttin
  • 107,550
  • 65
  • 332
  • 414
StriplingWarrior
  • 135,113
  • 24
  • 223
  • 283