What is the proper method to format dates?
I have some DateTime
s in my model. I only want to extract the date in a particular view.
By formatting the data in my view instead of my model, I can give other views the flexibility to use DateTime
or just Date
.
How can I go about formatting my DateTime
in a Vi开发者_运维百科ew?
You could do
dateTime.ToShortDateString();
Or
dateTime.ToString("{0:d}");
Or
dateTime.ToString("MM/dd/yyyy");
@(String.Format("{0:M/d/yyyy}", Model.dt));
I may be missing something but it would just be
Model.DateTimeProperty.ToString("mm/dd/yyy")
Or whatever other format you want to use. Same as formatting it anywhere else.
I usually use -
DateTime date= DateTime.Now;
string onlyDate=date.ToShortDateString();
精彩评论