How To Saved Date In Different Format In View?

时间:2017-10-12 09:56:19

标签: c# asp.net-mvc date datetime razor

Here I am trying to dislay date saved in CommentDate column which datatype is datetime in a format Oct 10, 2017 5:55 PM for which I have displayed date in view in @item.CommentDate.ToString("MMM d, yyyy h:mm tt") format .
Now the problem is that as i load page it shows error "No overload for method 'ToString' takes 1 arguments" in view in ToString("MMM d, yyyy h:mm tt") of @item.CommentDate.ToString("MMM d, yyyy h:mm tt"). Any help will be appreciated

2 个答案:

答案 0 :(得分:0)

What is the type of CommentDate, is it a dynamic? If so, you need to cast it to a DateTime first, as in:

@((DateTime)item.CommentDate).ToString("MMM d, yyyy h:mm tt")

答案 1 :(得分:0)

Try this. This will do it all. :-)

var d = DateTime.Now.ToString("MMMM dd, yyyy hh:mm tt");