Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert month int to month name

I was simply trying to use the DateTime structure to transform an integer between 1 and 12 into an abbrieviated month name.

Here is what I tried:

DateTime getMonth = DateTime.ParseExact(Month.ToString(),                         "M", CultureInfo.CurrentCulture); return getMonth.ToString("MMM"); 

However I get a FormatException on the first line because the string is not a valid DateTime. Can anyone tell me how to do this?

like image 478
Alex Hope O'Connor Avatar asked Jun 09 '11 00:06

Alex Hope O'Connor


People also ask

How can get month name from month number in SQL?

In MySQL, we can use a combination of functions 'MONTHNAME' and 'STR_TO_DATE' functions to get a month name from a month number. SQL SERVER: In SQL SERVER, we can use a combination of functions 'DATENAME' and 'DATEADD' functions to get a month name from a month number.

How do I get the month name from a number in Python?

To get the month name from a number in Python, the easiest way is with strftime() and passing “%M”. You can also use the calendar module and the month_name() function.


2 Answers

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1); 

See Here for more details.

Or

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

Or if you want to get the culture-specific abbreviated name.

GetAbbreviatedMonthName(1); 

Reference

like image 185
CharithJ Avatar answered Oct 16 '22 16:10

CharithJ


var monthIndex = 1; return month = DateTimeFormatInfo.CurrentInfo.GetAbbreviatedMonthName(monthIndex); 

You can try this one as well

like image 23
xei2k Avatar answered Oct 16 '22 15:10

xei2k