如何将可空的DateTime dt2转换为格式化的字符串?

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy-MM-dd hh:mm:ss")); //works

DateTime? dt2 = DateTime.Now;
Console.WriteLine(dt2.ToString("yyyy-MM-dd hh:mm:ss")); //gives following error:

ToString方法没有重载 一个参数


当前回答

. datetime != null ?((DateTime)s.SendDateTime).ToString("HH:mm:ss"): null

其他回答

简单的通用扩展

public static class Extensions
{

    /// <summary>
    /// Generic method for format nullable values
    /// </summary>
    /// <returns>Formated value or defaultValue</returns>
    public static string ToString<T>(this Nullable<T> nullable, string format, string defaultValue = null) where T : struct
    {
        if (nullable.HasValue)
        {
            return String.Format("{0:" + format + "}", nullable.Value);
        }

        return defaultValue;
    }
}

像这样简单的事情怎么样:

String.Format("{0:dd/MM/yyyy}", d2)

你们把事情搞得太复杂了。重要的是,停止使用ToString,开始使用字符串格式,比如string。支持字符串格式的格式或方法,如Console.WriteLine。下面是这个问题的最佳解决方案。这也是最安全的。

更新:

我用当前c#编译器的最新方法更新了示例。条件操作符和字符串插值

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://learn.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://learn.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}'");

输出:(我在里面放了单引号,所以你可以看到它返回为空字符串时为null)

'2019-04-09 08:01:39'
''
2019-04-09 08:01:39

'2019-04-09 08:01:39'
''

你可以使用简单的线条:

dt2.ToString("d MMM yyyy") ?? ""

下面是Blake作为一种扩展方法给出的优秀答案。将此添加到项目中,问题中的调用将按预期工作。 这意味着它的使用类似MyNullableDateTime.ToString("dd/MM/yyyy"),输出与MyDateTime.ToString("dd/MM/yyyy")相同,只是如果DateTime为空,则该值将为"N/A"。

public static string ToString(this DateTime? date, string format)
{
    return date != null ? date.Value.ToString(format) : "N/A";
}