How can I convert the nullable DateTime dt2 to a formatted string?
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString(\"yyyy-MM-dd hh
Console.WriteLine(dt2 != null ? dt2.Value.ToString("yyyy-MM-dd hh:mm:ss") : "n/a");
EDIT: As stated in other comments, check that there is a non-null value.
Update: as recommended in the comments, extension method:
public static string ToString(this DateTime? dt, string format)
=> dt == null ? "n/a" : ((DateTime)dt).ToString(format);
And starting in C# 6, you can use the null-conditional operator to simplify the code even more. The expression below will return null if the DateTime?
is null.
dt2?.ToString("yyyy-MM-dd hh:mm:ss")
Try this on for size:
The actual dateTime object your looking to format is in the dt.Value property, and not on the dt2 object itself.
DateTime? dt2 = DateTime.Now;
Console.WriteLine(dt2.HasValue ? dt2.Value.ToString("yyyy-MM-dd hh:mm:ss") : "[N/A]");
Seeing that you actually want to provide the format I'd suggest to add the IFormattable interface to Smalls extension method like so, that way you don't have the nasty string format concatenation.
public static string ToString<T>(this T? variable, string format, string nullValue = null)
where T: struct, IFormattable
{
return (variable.HasValue)
? variable.Value.ToString(format, null)
: nullValue; //variable was null so return this value instead
}
Shortest answer
$"{dt:yyyy-MM-dd hh:mm:ss}"
Tests
DateTime dt1 = DateTime.Now;
Console.Write("Test 1: ");
Console.WriteLine($"{dt1:yyyy-MM-dd hh:mm:ss}"); //works
DateTime? dt2 = DateTime.Now;
Console.Write("Test 2: ");
Console.WriteLine($"{dt2:yyyy-MM-dd hh:mm:ss}"); //Works
DateTime? dt3 = null;
Console.Write("Test 3: ");
Console.WriteLine($"{dt3:yyyy-MM-dd hh:mm:ss}"); //Works - Returns empty string
Output
Test 1: 2017-08-03 12:38:57
Test 2: 2017-08-03 12:38:57
Test 3:
You guys are over engineering this all and making it way more complicated than it really is. Important thing, stop using ToString and start using string formatting like string.Format or methods that support string formatting like Console.WriteLine. Here is the preferred solution to this question. This is also the safest.
I update the examples with up to date methods of today's C# compiler. conditional operators & string interpolation
DateTime? dt1 = DateTime.Now;
DateTime? dt2 = null;
Console.WriteLine("'{0:yyyy-MM-dd hh:mm:ss}'", dt1);
Console.WriteLine("'{0:yyyy-MM-dd hh:mm:ss}'", dt2);
// New C# 6 conditional operators (makes using .ToString safer if you must use it)
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-conditional-operators
Console.WriteLine(dt1?.ToString("yyyy-MM-dd hh:mm:ss"));
Console.WriteLine(dt2?.ToString("yyyy-MM-dd hh:mm:ss"));
// New C# 6 string interpolation
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated
Console.WriteLine($"'{dt1:yyyy-MM-dd hh:mm:ss}'");
Console.WriteLine($"'{dt2:yyyy-MM-dd hh:mm:ss}'");
Output: (I put single quotes in it so you can see that it comes back as a empty string when null)
'2019-04-09 08:01:39'
''
2019-04-09 08:01:39
'2019-04-09 08:01:39'
''
you can use simple line:
dt2.ToString("d MMM yyyy") ?? ""