views:

1937

answers:

5

Is there a best way to turn an integer into its month name in .net?

Obviously I can spin up a datetime to string it and parse the month name out of there. That just seems like a gigantic waste of time.

+6  A: 

Why not just use somedatetime.ToString("MMMM")?

leppie
So anytime I need to turn 1 into January, I need to new up a date time with an arbitrary year and day, in order to just get January?
DevelopingChris
That is correct, as a bonus, you can have it in any localizable language you want :)
leppie
+1  A: 

you can use a static method from the Microsoft.VisualBasic namespace:

string monthName = Microsoft.VisualBasic.DateAndTime.MonthName(monthInt, false);

Tokabi
+13  A: 

CultureInfo.DateTimeFormat.MonthNames[index]

Ovidiu Pacurar
Thats a good one too :)
leppie
This doesn't work. Nick Berardi provided the correct answer.
raven
This is where the functionality is, if you want me to write code for you ...
Ovidiu Pacurar
from now I will try to compile my answers first
Ovidiu Pacurar
+22  A: 

Try GetMonthName from DateTimeFormatInfo

http://msdn.microsoft.com/en-us/library/system.globalization.datetimeformatinfo.getmonthname.aspx

You can do it by:

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(1);
Nick Berardi
+1  A: 

To get abbreviated month value, you can use Enum.Parse();

Enum.Parse(typeof(Month), "0");

This will produce "Jan" as result.

Remember this is zero-based index.

Nelson