I have the following code in my MVC 4 razor view, which is supposed to take the DateTime property called modifiedDate, and print out the month name, ie, Jan, Feb etc, instead of 1, 2.
@foreach (var post in Model.NewsList)
{
<li class="entry">
<p class="calendar"><em>@post.modifiedDate.Value.Month.ToString("mmm")</em></p>
</li>
}
Any ideas how to do this?
Thanks.
// to get the Abbreviated month name string month_name = date. ToString("MMM"); // to get the full month name string month_name = date. ToString("MMMM"); Step 4: This string contains the name of the month.
DateTime dt = DateTime. Now; Console. WriteLine( dt. ToString( "MMMM" ) );
string k=DateTime. Now. Month. ToString();
You could simply use string. SubString - that's trivial - but don't. Instead, parse the date input using DateTime. TryParse and the user culture to get a DateTime, and get the month from that.
See The "MMMM" Custom Format Specifier
Usage:
DateTime? date = DateTime.Now;
date.Value.ToString("MMMM"); // Prints the Month name (e.g. May)
Corrected:
@foreach (var post in Model.NewsList)
{
<li class="entry">
<p class="calendar"><em>@post.modifiedDate.Value.ToString("MMMM")</em></p>
</li>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With