120

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?

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Mark Rogers
  • 96,497
  • 18
  • 85
  • 138

12 Answers12

131

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.

Matt Enright
  • 7,245
  • 4
  • 33
  • 32
Andy Mikula
  • 16,796
  • 4
  • 32
  • 39
  • 15
    That's not an enum though. – Scott Wisniewski May 22 '09 at 19:28
  • 3
    Thanks, interesting answer, not what I was looking for, but still worth a +! – Mark Rogers May 22 '09 at 19:29
  • 1
    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 May 22 '09 at 19:30
  • 4
    I agree about the culture specific issue, but why then did microsoft create DayOfWeek, that's culture-specific. Funny, huh? – Mark Rogers May 22 '09 at 19:33
  • Yeah, I agree that's weird, too. Crazy stuff! – Andy Mikula May 22 '09 at 20:14
  • 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 Sep 15 '09 at 13:50
  • 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 '10 at 19:58
  • 13
    If you are a culturally sensitive, time traveling programmer, be aware that there have been cultures that had a decimal week (eg Revolutionary France) http://en.wikipedia.org/wiki/Decimal_calendar – Matthew Lock Jun 08 '12 at 05:24
88

No, there isn't.

David Nelson
  • 3,666
  • 1
  • 21
  • 24
  • 4
    What about `Microsoft.VisualBasic.MonthName`? – Josh Stodola Jan 19 '10 at 16:57
  • MonthName returns a string value. – Mark Rogers Jan 19 '10 at 22:12
  • 24
    What a useful answer you have provided. Perhaps some additional guidance would have been more appropriate. – Michael Eakins Sep 20 '12 at 13:08
  • The answer is too direct. Could you put any reference, at least? – Guillermo Gutiérrez Oct 29 '12 at 22:16
  • 2
    @guillegr123: lol I think a direct answer is better than a roundabout answer. – user541686 Nov 15 '12 at 01:05
  • 16
    @guillegr123 How would you suggest that I go about proving a negative? – David Nelson Nov 21 '12 at 20:57
  • 9
    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 '15 at 08: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 '16 at 10:15
  • 1
    This should be removed ! – Jim Dec 09 '16 at 09:02
54

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
}
wal
  • 17,409
  • 8
  • 74
  • 109
  • 1
    Why use `NotSet`? Why Not just start with `January = 1`? then you could do away with the rest of the assignments too.. – Tersosauros Mar 06 '16 at 05: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 06 '16 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 '16 at 15:50
  • 10
    @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 '16 at 14:00
  • came to post the same thing.. this should be the answer. the Enum can be used against all DateTime functions i.e DateTime.Now.Month == (int)Month.January – Sonic Soul Sep 08 '17 at 14:48
  • 1
    Agreed I always use Unknown = 0 – David Homer Oct 17 '17 at 09:34
  • 1
    Whether you need `NotSet` or not would depend on how you intend to use the enum in the first place. If you are only ever going to use it in collections for example, e.g. `IList`, then the `NotSet` seems quite superfluous. – bytedev Apr 15 '20 at 06:18
  • Agree with comments above by oteal and bytedev. If you want to represent a not set value, you should instead implement as a nullable enum (Month?). The approach above implies that there is a month known as "NotSet". All consuming code would have to be aware that there is an NotSet option there, and handle appropriately... _every time_. Else, you could easily end up with a seemingly valid value coming in of, say: "21st NotSet, 1991". Smelly. Note how [DayOfWeek](https://learn.microsoft.com/en-us/dotnet/api/system.dayofweek?view=net-6.0) enum does not have an unknown or not set value. – Barnaby Dove May 03 '22 at 12:44
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 });
Doug Lampe
  • 1,426
  • 15
  • 8
  • 1
    return Enumerable.Range(0, 11) .Select(m => new {Id = month + 1, Name = DateTimeFormatInfo.CurrentInfo.MonthNames[month] }); – Keith Harrison Nov 15 '13 at 15:15
  • 1
    `var months = Enumerable.Range(0, 11).Select(m => new KeyValuePair(m + 1, DateTimeFormatInfo.CurrentInfo.MonthNames[m]));` – Dan Diplo Dec 08 '17 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.

vidalsasoon
  • 4,365
  • 1
  • 32
  • 40
  • 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 May 22 '09 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. – vidalsasoon May 22 '09 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. – Mark Rogers May 27 '09 at 21:34
  • +1 would be for effort for sure. What dll are we talking about here? – indolentdeveloper Jul 10 '13 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](https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-3.5/bb353140(v=vs.90)). – General Grievance Nov 02 '21 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...

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
  • 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. – Mark Rogers May 22 '09 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) – Charles Bretana Jan 12 '10 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
Community
  • 1
  • 1
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
  • 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. – Mark Rogers Jan 19 '10 at 22:11
  • @Mark: System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames is a string[] which is IEnumerable. – user169771 Aug 04 '15 at 20:43
  • 1
    @user169771 - string[] is an `IEnumerable` but its not a `enum`eration . So it doesn't work. – Mark Rogers Aug 04 '15 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

Jadawin
  • 29
  • 1
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

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

z zz
  • 31
  • 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: https://stackoverflow.com/help/how-to-answer . Good luck – nima Nov 02 '21 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 '22 at 18:30
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.

Scott Wisniewski
  • 24,561
  • 8
  • 60
  • 89
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.

Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
keith
  • 21