17

I have variables like:

DateTime crd = a.CreationDate; (shown as a variable in C# but available in razor views)

I want to show these as a date using the format: 11/06/2011 02:11

Ideally I would like to have some kind of HTML helper for this. Anyone out there already have something that might meet my needs?

tereško
  • 58,060
  • 25
  • 98
  • 150
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 1
    possible duplicate of [Converting DateTime format using razor](http://stackoverflow.com/questions/4679352/converting-datetime-format-using-razor) – Damith Nov 06 '11 at 10:00

7 Answers7

27

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
Community
  • 1
  • 1
JustinStolle
  • 4,182
  • 3
  • 37
  • 48
  • Make sure you also specify ApplyFormatInEditMode = true for the DisplayFormat attribute. – arni Apr 12 '21 at 18:22
19

in your model you can set [DisplayFormat] attribute with formatting as you wish

[DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
pubilc DateTime CreationDate{ get; set }
Damith
  • 62,401
  • 13
  • 102
  • 153
  • 1
    Tried what you suggested with my own date format, so copied your extract attribute but it still doesn't work. Any ideas? – Thierry Nov 05 '16 at 17:57
  • 1
    Ok, spoke to quickly. I was using @Model.DateOfBirth but in order for the DisplayFormat attribute to work you need to use @Html.DisplayFor(model => model.DateOfBirth). – Thierry Nov 05 '16 at 18:00
  • This does not not work me. I am rendering the field like this: `` is that correct? – Mohammed Noureldin Dec 12 '17 at 03:46
  • you might need ApplyFormatInEditMode = true in the data annotation, like this... [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd MMM yyyy}")] – Stephen Angell Jan 08 '20 at 17:22
5

Use this code to format your date:

@string.Format("{0:ddd}",Convert.ToDateTime(Html.DisplayFor(model => model.Booking.BookingFromDate).ToString()))

If your date field with required attribute then you don't want to validate null value. Other wise you can use ternary operator

ianaya89
  • 4,153
  • 3
  • 26
  • 34
user4439128
  • 49
  • 1
  • 3
4

How about

@crd.ToShortDateString()

Ravi Ram
  • 24,078
  • 21
  • 82
  • 113
1

Try Razor View

@Html.Raw(item.Today_Date.Date.ToString("dd.MM.yyyy"))

Cagdas Bsn
  • 26
  • 3
0

In the interest of completeness, you could also use string interpolation. I don't think this was an available feature when this question was asked.

@($"{crd:dd/mm/yyyy HH:mm}")

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/interpolated

GJKH
  • 1,715
  • 13
  • 30
-2

If you have a list of dates and want to grab the first:

Begin Date: @string.Format("{0:MM/dd/yyyy}", Convert.ToDateTime(Model.aList.Where(z => z.Counter == 1).Select(z => z.Begin_date).First()))
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57