Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

Get month name from int in C#

I am trying to get the name of the month (such as March) from an int value (such as 3). I have found a MonthName method in VB.NET, but can't find a similar method in C#.

I could create a DateTime object using the int for the month and then use some formatting options to get the month name from that, but I wondering if there is any methods similar to the VB.NET MonthName.

Ben
Tuesday, May 27, 2003

.NET is about language agnosticism. Just go ahead and call MonthName. It's a method off of Microsoft.VisualBasic.DateAndTime. Look it up in MSDN.

Brad Wilson (dotnetguy.techieswithcats.com)
Tuesday, May 27, 2003

Thanks Brad, that works if I have a method in my code behind that calls the MonthName method, but I can't call the MonthName directly from the aspx page, has to be in the code behind or I get a "The type or namespace name 'DateAndTime' does not exist in the class or namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)" error.

I am using the full reference - <%# Microsoft.VisualBasic.DateAndTime.MonthName((int)DataBinder.Eval(Container.DataItem, "TheMonth"), false) %>

Doesn't really matter, I was just wondering why I couldn't do it this way?

Ben
Tuesday, May 27, 2003

"I am using the full reference - <%# Microsoft.VisualBasic.DateAndTime.MonthName"

Well actually you are using a fully-qualified *name* BUT you likely have not actually *referenced* the required assembly. Add a reference in you project to Microsoft.VisualBasic by right-clicking the References icon in the project.

Alternatively do this:

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

Duncan Smart
Wednesday, May 28, 2003

In fact, forget all that, all you need to do in your code is this:

<%# DataBinder.Eval(Container.DataItem, "TheMonth", "{0:MMMM}")) %>

Duncan Smart
Wednesday, May 28, 2003

Sorry, too many parentheses:
<%# DataBinder.Eval(Container.DataItem, "TheMonth", "{0:MMMM}") %>

Duncan Smart
Wednesday, May 28, 2003

Thanks for your replies Duncan, but there are a few things I should point out.

Yes, I have references the required assembly, if I hadn't when I tried to call the Microsoft.VisualBasic.DateAndTime.MonthName method in my code behind I would get a compile time error.

As I said in my original question, yes, I could just create a method like you have with GetMonthName.

And with your other 2 posts, I am trying to convert an int to a month name, not a DateTime object, so the MMMM formating option doesn't work (that I know of).

So the question still remains, why can I call the Microsoft.VisualBasic.DateAndTime.MonthName from my code behind, but not my aspx page?

Ben
Wednesday, May 28, 2003

Your .aspx file is compiled when the page is accessed. The reference list in your project does not apply. What matters is the references made in your page.

Add the following directive to your .aspx file:

<%@ Assembly name="Microsoft.VisualBasic" %>

If you plan to use an assembly in many of your .aspx pages, you can add this directive to your global.asax file, and then all your pages will include the assembly reference.

Good luck...

Brad Wilson (dotnetguy.techieswithcats.com)
Thursday, May 29, 2003

Ah ha, that makes perfect sense, thanks Brad.

Ben
Thursday, May 29, 2003

The other hackish way would be to

DateTime foo = System.DateTime.Parse("01-JAN-1970");
DateTime bar = foo.AddMonths(intRepresentingMonths);
string monthName = bar.ToString("MMMM");
return monthName;


or on one line


return System.DateTime.Parse("01-JAN-1970").AddMonths(intMonth).ToString("MMMM");


This assumes that intMonth is zero-based.  Otherwise, use intMonth - 1.  If you're doing it repeatedly, you want to store the results of DateTime.Parse(), natch.

Richard Ponton
Friday, May 30, 2003

You could just use:
System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormatInfo.MonthNames[i-1]

Clue
Friday, July 11, 2003

*  Recent Topics

*  Fog Creek Home