Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Razor Get Month Name

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.

like image 731
tcode Avatar asked May 28 '13 15:05

tcode


People also ask

How to get current month name c#?

// 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.

How to convert month number to month name in c#?

DateTime dt = DateTime. Now; Console. WriteLine( dt. ToString( "MMMM" ) );

How to get month number from date in c#?

string k=DateTime. Now. Month. ToString();

How to get month from date in asp net c#?

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.


1 Answers

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>
}
like image 100
Dustin Kingen Avatar answered Sep 30 '22 05:09

Dustin Kingen