117

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, i.e. January, February, etc?

Does anyone know?

12 Answers 12

130

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

CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName (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.

8
  • 15
    That's not an enum though. May 22, 2009 at 19:28
  • 3
    Thanks, interesting answer, not what I was looking for, but still worth a +! May 22, 2009 at 19:29
  • 4
    I agree about the culture specific issue, but why then did microsoft create DayOfWeek, that's culture-specific. Funny, huh? May 22, 2009 at 19:33
  • 12
    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.
    – Ryan Lundy
    Jan 12, 2010 at 19:58
  • 11
    If you are a culturally sensitive, time traveling programmer, be aware that there have been cultures that had a decimal week (eg Revolutionary France) en.wikipedia.org/wiki/Decimal_calendar Jun 8, 2012 at 5:24
89

No, there isn't.

9
  • 4
    What about Microsoft.VisualBasic.MonthName? Jan 19, 2010 at 16:57
  • 23
    What a useful answer you have provided. Perhaps some additional guidance would have been more appropriate. Sep 20, 2012 at 13:08
  • 16
    @guillegr123 How would you suggest that I go about proving a negative? Nov 21, 2012 at 20:57
  • 8
    Perhaps it doesn't prove the negative, but a little reasoning wouldn't be out of order: There is no enum because enums can't be localized. Months.January would have little if no meaning to a Russian programmer. This is further supported by looking at the functions that DO provide month names - they are located on a Culture object.
    – Sam Axe
    Oct 19, 2015 at 8:18
  • 14
    Upvoting to counteract the 10 downvotes for the only actual answer to this question. Is there an official enumeration for months in .NET? No, there isn't. It would make little sense because not every calendar has the same number of months, and that's maybe why it has never been introduced. But that's not the point here. Is there one? No. Question answered correctly.
    – s.m.
    Jan 21, 2016 at 10:15
52

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

No.

Heres one I prepared earlier. (C# Version)

public enum Month
{
    NotSet = 0,
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
}
8
  • 1
    Why use NotSet? Why Not just start with January = 1? then you could do away with the rest of the assignments too.. Mar 6, 2016 at 5:54
  • 2
    @Tersosauros because then it will default to January if, for example, its used as a property for a class. in most cases i dont want that, i want it to have a 'null' value before it is explicitly assigned
    – wal
    Mar 6, 2016 at 12:34
  • 3
    But @wal, if you want 'null' value, you should use your enum as nullable. In your classe use public Month? Month { get; set; }.
    – oteal
    May 18, 2016 at 15:50
  • 9
    @Tersosauros note that 0 will always be the default value of an enum, even if it is not explicitly defined. If NotSet is omitted and January = 1 (and the rest of the values are 2, 3, etc.) then the default of this enum would be (Month)0
    – tsemer
    Nov 16, 2016 at 14:00
  • 1
    Agreed I always use Unknown = 0 Oct 17, 2017 at 9:34
32

DateTimeFormatInfo.CurrentInfo.MonthNames (not an enum, but I think that CurrentInfo instance of DateTimeFormatInfo is what you are looking for in general). If you want a drop-down list you could build it like this:

List<string> monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames.Take(12).ToList();
var monthSelectList = monthNames.Select(
   m => new { Id = monthNames.IndexOf(m) + 1, Name = m });
2
  • 1
    return Enumerable.Range(0, 11) .Select(m => new {Id = month + 1, Name = DateTimeFormatInfo.CurrentInfo.MonthNames[month] }); Nov 15, 2013 at 15:15
  • 1
    var months = Enumerable.Range(0, 11).Select(m => new KeyValuePair<int, string>(m + 1, DateTimeFormatInfo.CurrentInfo.MonthNames[m]));
    – Dan Diplo
    Dec 8, 2017 at 12:02
14

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

The location kinda scares but it's there nonetheless.

5
  • 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. May 22, 2009 at 19:37
  • 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. May 22, 2009 at 19:55
  • 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. May 27, 2009 at 21:34
  • +1 would be for effort for sure. What dll are we talking about here? Jul 10, 2013 at 19:51
  • It's cool that this existed at some point, but sadly, it seems to have gotten dropped later on. It was was part of Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.dll. The enumeration is documented here. Nov 2, 2021 at 15:41
11

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...

2
  • 1
    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. May 22, 2009 at 19:32
  • 1
    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) Jan 12, 2010 at 21:30
3

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
3
  • 1
    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. Jan 19, 2010 at 22:11
  • @Mark: System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames is a string[] which is IEnumerable.
    – user169771
    Aug 4, 2015 at 20:43
  • 1
    @user169771 - string[] is an IEnumerable but its not a enumeration . So it doesn't work. Aug 4, 2015 at 20:46
2

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

0
2

Some calender do indeed have more than 12 months: http://en.wikipedia.org/wiki/Month but I can't say if it was the reason MS did not built an enum in .NET.

For the lazy like me who would have liked a copy/paste, in VB:

Public Enum MonthsOfYear
    January = 1
    February = 2
    March = 3
    April = 4
    May = 5
    June = 6
    July = 7
    August = 8
    September = 9
    October = 10
    November = 11
    December = 12
End Enum
1

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.

1

[0 - 11] var MonthNames = new List<string>(DateTimeFormatInfo.CurrentInfo.MonthNames);

2
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. You can find more information on how to write good answers in the help center: stackoverflow.com/help/how-to-answer . Good luck 🙂
    – nima
    Nov 2, 2021 at 15:14
  • I had a look at this, the DateTimeFormatInfo.CurrentInfo.MonthNames string array actually contains an empty string entry at the end, meaning the array length is 13, so.. heads up.
    – Sam Jones
    Nov 22, 2022 at 18:30
0

An enum would be rather useful, but you can get the desired result with a format:

DateTime myDateTimeObject=DateTime.Now; //(for example)
string monthName = myDateTimeObject.ToString("MMMM");

This returns the full month name (January, February, etc.). Use myDateTimeObject.ToString("MMM") for short name (Jan, Feb, Mar, etc.).

If you have a particular month number, mnthNum, without any DateTime, you could always use something like this:

string monthName=(new DateTime(2000,mnthNum,1)).ToString("MMMM");

or

string monthName=((new DateTime(2000,1,1)).AddMonths(mnthNum-1)ToString("MMMM");

But that seems a little messy. The first example requires that mnthNum is between 1 and 12. The second example allows for (almost) any month number and is not restricted to 1 to 12.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.