-1

How to render a DateTime in a specific format in ASP.NET MVC 3 using @Html.DisplayFor(.....)? If I have in my model class a property of type DateTime how can I render it in a specific format.

Data Annotation:
        [DisplayName("Created On")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
        public object CreatedOn { get; set; }

View:

  @Html.DisplayFor(modelItem => item.CreatedOn)

it displaying only Date in mm/dd/yyyy format, but i want dd/mm/yyyy hh: mm:ssss or mm/dd/yyyy hh: mm:ssss in this format.

3
  • Please refer to this questions stackoverflow.com/questions/6001654/… Commented Aug 19, 2013 at 10:01
  • I tried @Html.DisplayFor(modelItem => item.MyDateTime.ToLongDateString()), but it is not working Commented Aug 19, 2013 at 10:30
  • See my answer for an alternative solution Commented Aug 19, 2013 at 10:41

4 Answers 4

1

If your requirement just showing date and time in the format of dd/mm/yyyy hh: mm:ssss, no need to use DisplayFor(), use like below it displays date and time

 @item.CreatedOn
Sign up to request clarification or add additional context in comments.

Comments

1

You can use DisplayFormat attribute, see MSDN Ref

[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", 
                                                  ApplyFormatInEditMode = true)]
public DateTime YourDateTime { get; set; }

4 Comments

@PKKG Attribute parameters must be constants. For an application that handles multiple cultures, the date format string is not constant. Therefore this method using attributes is not a good solution for multi-cultural applications.
@PKKG If the format was hard-coded in the model, or if the application was designed only for 'dd/MM/yyyy', then the application is NOT multi-cultural. My comment of "This isn't a good solution for multi-cultural applications." still stands true for this solution.
Stop it guys Oliver and PKKG, You both are correct in there respective thinking. Lets have peace
@PKKG & Satpal All friendly here, just clarifying my point :). The difference is that in my example, the string can be replaced with a variable containing the date format of the user, whereas this is not possible with attribute parameters.
0

If you want to render the DateTime in a SPECIFIC format, you could just render the DateTime using ToString() and pass the format string that you require:

@Model.DateField.ToString("dd/MM/yyyy")

Otherwise, @Html.DisplayFor(model => model.DateField) will use the format defined in the thread's culture.

The thread's culture can be set to automatically use the client's culture (using the Accept-Language request header) in the web.config file:

  <system.web>
    <globalization enableClientBasedCulture="true" />
  </system.web>

8 Comments

It is showing error like ToString() does not have an overloaded method that takes 1 argument
By the looks of it, you have changed your data type to object, this should be DateTime. Change it back to DateTime and this error should stop.
Its not suitable for my requirement
No change in output whether data type is object or DateTime
Did you remember to build the solution?
|
0

You can use the ToString() method for your problem.

Datevalue.ToString("dd.MM.yyyy");

or

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

4 Comments

It is showing error like ToString() does not have an overloaded method that takes 1 argument
Then your object does not seem to be a DateTime
No change in output whether data type is object or DateTime
please delete [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] in your annotation and try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.