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
?
11 Answers
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.

- 647,829
- 179
- 1,238
- 2,067
-
5Unless 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
string date = myVariable.HasValue ? myVariable.Value.ToString() : string.Empty;

- 12,251
- 8
- 58
- 92
-
16What!? 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
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>

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

- 733,204
- 149
- 1,241
- 1,454
-
4Or better yet: make it generic: `public static string ToSafeString
(this T? obj) where T : struct` :) – JulianR Sep 18 '09 at 17:51 -
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() : "";
}

- 6,594
- 9
- 63
- 100
DateTime? d;
// stuff manipulating d;
return d != null ? d.Value.ToString() : String.Empty;

- 4,962
- 1
- 29
- 31
DateTime? MyNullableDT;
....
if (MyNullableDT.HasValue)
{
return MyNullableDT.Value.ToString();
}
return "";

- 83,810
- 28
- 209
- 234

- 1,220
- 12
- 28
if (aDate.HasValue)
return aDate;
else
return string.Empty;

- 83,810
- 28
- 209
- 234

- 5,437
- 7
- 45
- 62
DateTime d?;
string s = d.HasValue ? d.ToString() : string.Empty;

- 136,852
- 88
- 292
- 341
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.

- 239,200
- 50
- 490
- 574

- 39
- 2
-
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