Friday, December 26, 2008

How to convert int Month to String Month Name in C#?

Often we get month as integer from external source and want to display it as "Oct" or "October" i.e. string. Following is a code to convert the int month to string month :)

 
/// <summary>
/// Converts the given month int to month name
/// </summary>
/// <param name="month">month int </param>
/// <param name="abbrev">return abbreviated or not</param>
/// <returns>Short or long month name as string </returns>
private static string GetMonthName(int month, bool abbrev)
{
DateTime date = new DateTime(2000, month, 1);
if (abbrev) return date.ToString("MMM");
return date.ToString("MMMM");
}
Hope this helps :)

4 comments:

Anonymous said...

Hi Sandeep,

Another culture-specific way is:

System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month);

where month is an integral value. You can also substitute CurrentCulture for any other CultureInfo object you like!

Sandeep Aparajit said...

Thanks for your comment..
Thats great info Nic..

-Sandeep

reg said...

Thanks! Just what I was looking for!

Anonymous said...

no working this code thank you

Post a Comment