0
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0: dd-mm-yyyy}")]
public DateTime DateProcessed { get; set; }

This is in my Data Model and it's giving is 01/01/2015

But I need in this format January/01/2015 result.

I have tried using the ToShortDateString(). But I am having

error Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.

As much as possible. I want it to be in the models instead of using the View. But if it can't be help.

What ever works is fine.

Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28
Aizen
  • 1,807
  • 2
  • 14
  • 28

2 Answers2

4

Try this;

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0: MMMM/dd/yyyy}")]
public DateTime DateProcessed { get; set; }

Reference: here.

If you are, however, stuck to a single dateformat, you can also "hack" your View without touching the model.

Something like this;

<div>
  <dl class="dl-horizontal">
     <dt>@Html.DisplayNameFor(model => model.DateProcessed)</dt>
     <dd>@Html.DisplayFor(model => model.DateProcessed)</dd>
  </dl>
</div>

Becomes;

<div>
  <dl class="dl-horizontal">
     <dt>@Html.DisplayNameFor(model => model.DateProcessed)</dt>
     <dd>
         @{string formattedDate = Model.DateProcessed.ToString("MMMM/dd/yyyy");}
         @Html.DisplayFor(model => formattedDate)
     </dd>
  </dl>
</div>

Which will show the date in the correct format.

Dion V.
  • 2,090
  • 2
  • 14
  • 24
  • Wow, you updated your answer. I was waiting for the 10 mins so I can accept the answer. So I shut my eyes, then walla. Woke up 15 hours after. – Aizen May 07 '15 at 02:48
0

Try this :

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0: MMMM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime DateProcessed { get; set; }

And if you don't use date format in your model class then try below on :

First Set your date property as below

public Nullable<System.DateTime> DateProcessed { get; set; }

then your in your View like this :

@Html.TextBoxFor(m => m.DateProcessed,"{0: MMMM/dd/yyyy}", placeholder = "MMMM/dd/yyyy" })
Krunal Mevada
  • 1,637
  • 1
  • 17
  • 28