Reputation: 97707
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?
Upvotes: 122
Views: 75092
Reputation: 134
If you are looking for existing solusion, there is a predefined enum for months in a Microsoft.SqlServer.SqlManagementObjects package It has all you asked for, just import the package using nuget manager
Upvotes: 0
Reputation: 146499
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...
Upvotes: 11
Reputation: 31
[0 - 11]
var MonthNames = new List<string>(DateTimeFormatInfo.CurrentInfo.MonthNames);
Upvotes: 1
Reputation: 21
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.
Upvotes: 0
Reputation: 16780
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.
Upvotes: 131
Reputation: 17729
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
}
Upvotes: 59
Reputation: 1466
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 });
Upvotes: 33
Reputation: 23
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
Upvotes: 2
Reputation: 82483
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
Upvotes: 3
Reputation: 19
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
Upvotes: 1
Reputation: 4401
Found one in the enum "MonthNamesType" of this namespace: Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.Exchange2007
The location kinda scares but it's there nonetheless.
Upvotes: 14
Reputation: 25041
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.
Upvotes: 1