Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to turn an integer into a month name in c#?

Tags:

date

.net

parsing

People also ask

How do you convert int to month?

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

How can get month name from month number in SQL?

In SQL SERVER, we can use a combination of functions 'DATENAME' and 'DATEADD' functions to get a month name from a month number. Oracle: In Oracle, we can use a combination of functions 'TO_CHAR' and 'TO_DATE' functions to get a month name from a month number.


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);

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


Updated with the correct namespace and object:

//This was wrong
//CultureInfo.DateTimeFormat.MonthNames[index];

//Correct but keep in mind CurrentInfo could be null
DateTimeFormatInfo.CurrentInfo.MonthNames[index];

DateTime dt = new DateTime(year, month, day);
Response.Write(day + "-" + dt.ToString("MMMM") + "-" + year);

In this way, your month will be displayed by their name, not by integer.


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

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

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.