How to convert date time format in a Razor view? [

2019-09-10 10:09发布

问题:

This question already has an answer here:

  • Converting DateTime format using razor 9 answers

I need to display a date time string in the following format in a Razor view:

24 Jun'16

how can I convert the string in a Razor view?

回答1:

Just like you normally would:

@Model.YourDateTime.ToString("d MMM \\'yy")

The \\ escapes the ', since that is a preserved character. The rest is quite straightforward.

Another option is the use of display attributes with DisplayFor:

[DisplayFormat(DataFormatString = "{0:d MMM \\'yy}")]
public DateTime YourDateTime { get; set }