Get short date for System Nullable datetime (datetime ?) in C#

user2811143 picture user2811143 · Sep 24, 2013 · Viewed 39.8k times · Source

How to get short date for Get short date for System Nullable datetime (datetime ?)

for ed 12/31/2013 12:00:00 --> only should return 12/31/2013.

I don't see the ToShortDateString available.

Answer

Darren picture Darren · Sep 24, 2013

You need to use .Value first (Since it's nullable).

var shortString = yourDate.Value.ToShortDateString();

But also check that yourDate has a value:

if (yourDate.HasValue) {
   var shortString = yourDate.Value.ToShortDateString();
}