开发者

What is the proper method to format dates?

开发者 https://www.devze.com 2023-02-14 18:10 出处:网络
I have some DateTimes 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 o

I have some DateTimes 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消