37

I have a DateTime? variable, sometimes the value is null, how can I return an empty string "" when the value is null or the DateTime value when not null?

Joey
  • 344,408
  • 85
  • 689
  • 683
JL.
  • 78,954
  • 126
  • 311
  • 459

11 Answers11

111

Though many of these answers are correct, all of them are needlessly complex. The result of calling ToString on a nullable DateTime is already an empty string if the value is logically null. Just call ToString on your value; it will do exactly what you want.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • 5
    Unless you want to use the DateTime properties like `.Day` `.Week` because that will give you the entire DateTime string and lose the power of the DateTime class. e.g. `myVariable.Value.Hour.ToString()`. Just an example reason why you might want to do otherwise. – baron Aug 04 '11 at 06:01
  • 2
    @baron, those properties aren't `Nullable`. – Sam Sep 10 '13 at 04:37
41
string date = myVariable.HasValue ? myVariable.Value.ToString() : string.Empty;
Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
  • 16
    What!? You can just call .ToString() on the Nullable instance to get String.Empty. Even Eric Lippert (who might have even implemented this behavior) notes this. _That_ should be the accepted answer. – codekaizen Jul 09 '10 at 01:48
  • 1
    @codekaizen - I get an exception when I try that. So no, that would not be the accepted answer. Perhaps this is not a problem in more recent versions of c# or .net? – Kimball Robinson Aug 20 '10 at 16:13
  • 1
    @k.robinson - perhaps that is because you are using a boxed reference to the instance. Please realize that I'm advocating the same as Eric Lippert - one of the creators of the .Net platform itself - is pointing out in his answer. If you have a problem, you might want to reconsider if "select isn't broken" (http://www.pragprog.com/the-pragmatic-programmer/extracts/tips). – codekaizen Aug 20 '10 at 16:57
  • @codekaizen - Ok. I still wish I could do this without a typecast: dateTimeField.Text = dateTimeObj.HasValue ? ((DateTime) dateTimeObj).ToShortDateString() : string.Empty; – Kimball Robinson Aug 25 '10 at 00:12
18

Actually, this is the default behaviour for Nullable types, that without a value they return nothing:

public class Test {
    public static void Main() {
        System.DateTime? dt = null;
        System.Console.WriteLine("<{0}>", dt.ToString());
        dt = System.DateTime.Now;
        System.Console.WriteLine("<{0}>", dt.ToString());
    }
}

this yields

<>
<2009-09-18 19:16:09>
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 4
    +1 Did not know this. However, you cannot supply a formatting string this way. – Jon Seigel Sep 18 '09 at 17:20
  • Hm, right. Though this may not be a problem in this case. I didn't know this myself too until yesterday, though. Stumbled over it when looking at `Nullable` in Reflector :-) – Joey Sep 18 '09 at 17:27
8

Calling .ToString() on a Nullable<T> that is null will return an empty string.

marsze
  • 15,079
  • 5
  • 45
  • 61
DJ.
  • 1,005
  • 2
  • 15
  • 24
7

You could write an extension method

public static string ToStringSafe(this DateTime? t) {
  return t.HasValue ? t.Value.ToString() : String.Empty;
}

...
var str = myVariable.ToStringSafe();
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
3

All you need to do is to just simply call .ToString(). It handles Nullable<T> object for null value.

Here is the source of .NET Framework for Nullable<T>.ToString():

public override string ToString() {
    return hasValue ? value.ToString() : "";
}
Jalal
  • 6,594
  • 9
  • 63
  • 100
2
DateTime? d;
// stuff manipulating d;
return d != null ? d.Value.ToString() : String.Empty;
Cecil Has a Name
  • 4,962
  • 1
  • 29
  • 31
1
DateTime? MyNullableDT;
....
if (MyNullableDT.HasValue)
{
    return MyNullableDT.Value.ToString();
}
return "";
NullUserException
  • 83,810
  • 28
  • 209
  • 234
Ahmed Khalaf
  • 1,220
  • 12
  • 28
1
if (aDate.HasValue)
    return aDate;
else
    return string.Empty;
NullUserException
  • 83,810
  • 28
  • 209
  • 234
Mike
  • 5,437
  • 7
  • 45
  • 62
1
DateTime d?;
string s = d.HasValue ? d.ToString() : string.Empty;
Patrick Desjardins
  • 136,852
  • 88
  • 292
  • 341
0

According to Microsoft's documentation:

The text representation of the value of the current Nullable object if the HasValue property is true, or an empty string ("") if the HasValue property is false.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • This was already answered [here](https://stackoverflow.com/a/1446033/10927863) by [Eric Lippert](https://stackoverflow.com/users/88656/eric-lippert) – Eliahu Aaron Jun 18 '19 at 16:13