views:

2283

answers:

7

I'm looking to see if there is an official enumeration for months in the .net framework.

It seems possible to me that there is one, because of how common the use of month is, and because there are other such enumerations in the .net framework.

For instance, there is an enumeration for the days in the week, System.DayOfWeek, which includes monday, tuesday, etc..

I'm wondering if there is one for the months in the year, ie. January, February, etc?

Does anyone know?

Thanks for reading!

+9  A: 

No, there isn't.

What about `Microsoft.VisualBasic.MonthName`?
Josh Stodola
MonthName returns a string value.
Mark Rogers
+13  A: 

There isn't, but if you want the name of a month you can use:

CultureInfo.CurrentCulture.DateTimeFormat.GetMonth ( DateTime.Now.Month );

which will return a string representation (of the current month, in this case). Note that GetMonth takes arguments from 1 to 13 - January is 1, 13 is a blank string.

Andy Mikula
That's not an enum though.
Scott Wisniewski
Thanks, interesting answer, not what I was looking for, but still worth a +!
Mark Rogers
You're right, but having an enum for a culture-specific list like that is silly. This is a better solution to the problem, as far as I'm concerned.
Andy Mikula
I agree about the culture specific issue, but why then did microsoft create DayOfWeek, that's culture-specific. Funny, huh?
Mark Rogers
Yeah, I agree that's weird, too. Crazy stuff!
Andy Mikula
I can see why not create an enum since it is not culture specific, but like you guys said, why create one for the day of the week?
Jonas Stawski
Perhaps because various cultures have different monthly calendars, but there's (apparently) not one that has a different number of days per week. So the *number* of days per week is consistent even if the *names* aren't.
Kyralessa
+1  A: 

I don't know for sure, but my hunch is no. DateTime.Month returns an integer. If there was such an enumeration, it would probably be returned by DateTime.

Scott Wisniewski
+3  A: 

What exactly are you attempting to accomplish?

if all you want is twelve strings with the months of the year spelled out, then that is available via a custom format string - applied for any instance of a datetime,

  DateTime dt = DateTime.Parse("12 January 2009";
   dt.ToString("MMM");  // prints "Jan" 
                        // (or the right abbrev is in current culture)
   dt.ToString("MMMM"); // prints "January" 
                        // (or correct sp in current culture)

if you just want to be able to specify the month as an enumerated property of some other object type, then the Month property of a DateTime field returns an integer from 1 to 12...

Charles Bretana
Thanks for your answer, I'm just curious if there is an official enumeration, because I'm about to create one and I'd just like to know if there is one that already exists, so I don't redefine an unneeded enumeration.
Mark Rogers
No afaik, there is none, (If you do create one, I would make the integer values explicit, sop you can ensurethey are the same as the 1-12 values used by .Net to represent monthNumbers)
Charles Bretana
+4  A: 

Found one in the enum "MonthNamesType" of this namespace: Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.Exchange2007

The location kinda scares but it's there nonetheless.

vidalsasoon
I would award you the correct answer, but I don't think that is part of the base library .net framework. +1 for effort and creativity.
Mark Rogers
seems like it's part of .net 3.5. Just open the object viewer in Visual Studio and search for "January" and the matches in the API come up.
vidalsasoon
Yeah, I did as you suggested, it looks like it's in v3.0 folder, and it's not a default library. So while I guess it's in the library it's location is less than ideal. It's in something of a gray area as far as the requirements of the question.
Mark Rogers
+1  A: 

I would be looking for something like this to code with, as

        if (DateTime.Now.Month != 1) // can't run this test in January.

has this magic number of 1 in it. whereas

        if (DateTime.Now.Month != DateTime.MonthsOfYear.January) 

is self-documenting

Yeah, me too! Sadly there isn't a good way to do this.
Mark Rogers
+2  A: 

Yes, there certainly is. It's part of the Microsoft.VisualBasic namespace...

Microsoft.VisualBasic.MonthName

And for those of you that have a problem with this namespace, you should understand that it truly is .NET, and it is not going anywhere.

For the record, the MonthName function internally calls the following...

Thread.CurrentThread.CurrentCulture.DateTimeFormat.GetMonthName
Josh Stodola
Sorry but this answer has the same problem as previous answer, in that it is not an enumeration, which is the topic of this question.Still, thanks for the info.
Mark Rogers