51

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.

Darren
  • 68,902
  • 24
  • 138
  • 144
user2811143
  • 521
  • 1
  • 4
  • 4
  • OP - Always remember that when using generics the underlying class's methods are available if you are able to expose the underlying class. In this case using the `.Value` method of `System.Nullable` Take a look at my answer for further reference. – JNYRanger Sep 24 '13 at 13:11

7 Answers7

117

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();
}
Darren
  • 68,902
  • 24
  • 138
  • 144
22

string.Format("{0:d}", dt); works:

DateTime? dt = (DateTime?)DateTime.Now;
string dateToday = string.Format("{0:d}", dt);

Demo

If the DateTime? is null this returns an empty string.

Note that the "d" custom format specifier is identical to ToShortDateString.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 2
    This should be the accepted answer because it can handle null values gracefully. Optionally you could use string interpolation - DateTime? dt = (DateTime?)DateTime.Now; string dateToday = $"{dt:d}" – Chris M. Nov 07 '16 at 18:00
8

That function is absolutely available within the DateTime class. Please refer to the MSDN documentation for the class: http://msdn.microsoft.com/en-us/library/system.datetime.toshortdatestring.aspx

Since Nullable is a generic on top of the DateTime class you will need to use the .Value property of the DateTime? instance to call the underlying class methods as seen below:

DateTime? date;
String shortDateString;
shortDateString = date.Value.ToShortDateString();

Just be aware that if you attempt this while date is null an exception will be thrown.

JNYRanger
  • 6,829
  • 12
  • 53
  • 81
  • 3
    `DateTime != DateTime?` (`Nullable`). – Corak Sep 24 '13 at 13:02
  • @Corak aware of that, but since nullable is technically a generic you can still utulize the underlying class's methods. I should have added that you do need to use the Value property though to use the underlying class methods. I'll edit my post to make it more clear. – JNYRanger Sep 24 '13 at 13:04
6

If you want to be guaranteed to have a value to display, you can use GetValueOrDefault() in conjunction with the ToShortDateString method that other postelike this:

yourDate.GetValueOrDefault().ToShortDateString();

This will show 01/01/0001 if the value happened to be null.

Steve Danner
  • 21,818
  • 7
  • 41
  • 51
1

Check if it has value, then get required date

if (nullDate.HasValue)
{
     nullDate.Value.ToShortDateString();
}
Muhammad Umar
  • 3,761
  • 1
  • 24
  • 36
1

Try

    if (nullDate.HasValue)
    {
         nullDate.Value.ToShortDateString();
    }
0

If you are using .cshtml then you can use as

<td>@(item.InvoiceDate==null?"":DateTime.Parse(item.YourDate.ToString()).ToShortDateString())</td>

or if you try to find short date in action or method in c# then

yourDate.GetValueOrDefault().ToShortDateString();

And is already answered above by Steve.

I have shared this as i used in my project. it works fine. Thank you.