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

C#.NetStringDatetimeNullable

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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();
}

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

Check if it has value, then get required date

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

Solution 6 - C#

Try

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

Solution 7 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2811143View Question on Stackoverflow
Solution 1 - C#DarrenView Answer on Stackoverflow
Solution 2 - C#Tim SchmelterView Answer on Stackoverflow
Solution 3 - C#JNYRangerView Answer on Stackoverflow
Solution 4 - C#Steve DannerView Answer on Stackoverflow
Solution 5 - C#Muhammad UmarView Answer on Stackoverflow
Solution 6 - C#user6721009View Answer on Stackoverflow
Solution 7 - C#Er. Binod MehtaView Answer on Stackoverflow