转换DateTime?在dd-MM-yyyy中输入字符串

Pra*_*ruz 1 .net c# linq datetime date

我以完整的dateTime格式从db获取值,但需要将其转换为上述格式的字符串.我使用了dateTime.ParseExact,但似乎无法使用nullable type..please帮助

小智 14

使用可为空的DateTime对象的Value属性:

dt.HasValue ? dt.Value.ToString("dd-MM-yyyy") : string.Empty; 
Run Code Online (Sandbox Code Playgroud)

如果它为null,则将'string.Empty'替换为您要执行的任何操作.