Search code examples
datetimedatetime-formateditorformodel

Returning only date part of datetime string in editorfor template


I have the following code to try and return my datetime value from my Editorfor template in the dd/MM/yyyy format without the time value:

@Html.EditorFor(c => c.SelectedContent.ExpiryDate, "{0:dd/MM/yyyy }")

This however is returning also the time value but it is not what I need, how can I return this so it is only the date?


Solution

  • Place the following line above where you declare ExpiryDate in your model

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]