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

8

52

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.

Lactometer answered 24/9, 2013 at 12:59 Comment(1)
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.Kristoforo
B
119

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();
}
Barker answered 24/9, 2013 at 13:0 Comment(1)
And check if yourDate.HasValue first.Adena
E
23

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.

Extend answered 24/9, 2013 at 13:8 Comment(1)
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}"Buckjump
K
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.

Kristoforo answered 24/9, 2013 at 13:1 Comment(2)
DateTime != DateTime? (Nullable<DateTime>).Adena
@Adena 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.Kristoforo
U
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.

Unconformable answered 24/9, 2013 at 13:5 Comment(0)
I
2

A more concise way to do the same thing would be this:

var shortString = yourDate?.ToShortDateString();

It returns a null if it is a null and the short date if it is not null.

Instrumentalism answered 28/12, 2023 at 21:8 Comment(2)
I feel that you're on the right track here, but the code you've posted is incorrect. You really should write a small block of code that demonstrates the technique - actually run it to make sure it works - and then post that as part of your answer. You're close to having the best answer for this question but you've ham-fisted it.Sagittarius
@Sagittarius you are right. I have edited my answer and will try to be more careful in the future.Instrumentalism
B
1

Check if it has value, then get required date

if (nullDate.HasValue)
{
     nullDate.Value.ToShortDateString();
}
Binucleate answered 24/9, 2013 at 13:5 Comment(0)
E
1

Try

    if (nullDate.HasValue)
    {
         nullDate.Value.ToShortDateString();
    }
Elora answered 16/8, 2016 at 9:49 Comment(0)
R
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.

Relinquish answered 16/11, 2017 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.