P.Brian.Mackey
P.Brian.Mackey

Reputation: 44275

What is the proper method to format dates?

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 View?

Upvotes: 0

Views: 106

Answers (4)

Vishal
Vishal

Reputation: 12369

I usually use -

DateTime date= DateTime.Now; 
string onlyDate=date.ToShortDateString();

Upvotes: 1

raklos
raklos

Reputation: 28545

@(String.Format("{0:M/d/yyyy}", Model.dt));

Upvotes: 3

Brandon
Brandon

Reputation: 69953

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.

Upvotes: 2

Nate
Nate

Reputation: 30636

You could do

dateTime.ToShortDateString();

Or

dateTime.ToString("{0:d}");

Or

dateTime.ToString("MM/dd/yyyy");

Upvotes: 5

Related Questions