Is there a predefined enumeration for Month in the .NET library?

C#.NetDatetimeEnumeration

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

No, there isn't.

Solution 3 - C#

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

Solution 4 - C#

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

Solution 5 - C#

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

The location kinda scares but it's there nonetheless.

Solution 6 - C#

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

Solution 7 - C#

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

Solution 8 - C#

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

Solution 9 - C#

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

Solution 10 - C#

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.

Solution 11 - C#

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.

Solution 12 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark RogersView Question on Stackoverflow
Solution 1 - C#Andy MikulaView Answer on Stackoverflow
Solution 2 - C#David NelsonView Answer on Stackoverflow
Solution 3 - C#walView Answer on Stackoverflow
Solution 4 - C#Doug LampeView Answer on Stackoverflow
Solution 5 - C#vidalsasoonView Answer on Stackoverflow
Solution 6 - C#Charles BretanaView Answer on Stackoverflow
Solution 7 - C#Josh StodolaView Answer on Stackoverflow
Solution 8 - C#JadawinView Answer on Stackoverflow
Solution 9 - C#Francois GagnonView Answer on Stackoverflow
Solution 10 - C#Scott WisniewskiView Answer on Stackoverflow
Solution 11 - C#keithView Answer on Stackoverflow
Solution 12 - C#z zzView Answer on Stackoverflow