ali raza

C#, .Net ,Asp.net and my thoughts

How to get Month name from current date using c#

Hi i found two methods to do so.

Using Globliziation

System.Globalization.DateTimeFormatInfo d = new System.Globalization.DateTimeFormatInfo();
string month = d.MonthNames[DateTime.Now.Month-1];

Using new date object as follow

string MonthName = GetMonthName(DateTime.Now.Month);
static string GetMonthName(int monthNum)
{
return GetMonthName(monthNum, false);
}
static string GetMonthName(int monthNum, bool abbreviate)
{
if (monthNum < 1 || monthNum > 12)
throw new ArgumentOutOfRangeException(“monthNum”);
DateTime date = new DateTime(1, monthNum, 1);
if (abbreviate)
return date.ToString(“MMM”);
else
return date.ToString(“MMMM”);
}

Chears

One response to “How to get Month name from current date using c#

  1. gaurav March 26, 2009 at 1:01 pm

    thanx yaar…tht worked for me finally…..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: