How can I format a DateTime in a Razor view?
in your model you can set [DisplayFormat] attribute with formatting as you wish
[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
pubilc DateTime CreationDate{ get; set }
You could create a Display Template or Editor Template like in this answer, but the format would apply to all DateTime variables in the given scope (maybe good or bad).
Using the DisplayFormat attribute works well to define formats for individual fields.
Remember to use @Html.DisplayFor(model=>model.crd)
and/or @Html.EditorFor(model=>model.crd)
syntax for either of the above.
You can always use the DateTime.ToString()
method in your views as well for more ad hoc formatting.
@crd.ToString("MM/dd/yyyy HH:mm") // time using a 24-hour clock