MVC View nullable Date field formatting

2019-04-03 12:59发布

I am trying to display the following in a view but is giving a problem:

    <td>
     @item.CreatedByDt.ToString("MM/dd/yyyy") 
    </td>  

Any idea on how to handle a nullable Date field in a view. I am using Razor by the way.

I am getting the following error:

No overload for method 'ToString' takes 1 arguments

4条回答
欢心
2楼-- · 2019-04-03 13:31
@(item.CreatedByDt.HasValue ? item.CreatedByDt.Value.ToString("MM/dd/yyyy") : "--/--/----")

You can replace the display string for when your date is null with what you want.

查看更多
够拽才男人
3楼-- · 2019-04-03 13:32
@item.CreatedByDt.Value.ToString("MM/dd/yyyy")
查看更多
走好不送
4楼-- · 2019-04-03 13:42
@item.PostDatePublished.Value.ToString

need the value in there

查看更多
smile是对你的礼貌
5楼-- · 2019-04-03 13:45

If you don't know if it will be null or not...

@string.Format("{0:MM/dd/yyyy}", item.CreatedByDt)
查看更多
登录 后发表回答