How can I format a nullable DateTime with ToString()?

前端 未结 20 2491
面向向阳花
面向向阳花 2020-11-27 12:04

How can I convert the nullable DateTime dt2 to a formatted string?

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString(\"yyyy-MM-dd hh         


        
20条回答
  •  情书的邮戳
    2020-11-27 12:34

    You can use dt2.Value.ToString("format"), but of course that requires that dt2 != null, and that negates th use of a nullable type in the first place.

    There are several solutions here, but the big question is: How do you want to format a null date?

提交回复
热议问题