C# .NET 库中是否有预定义的 Month 枚举?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/899565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 22:08:15  来源:igfitidea点击:

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

c#.netdatetimeenumeration

提问by Mark Rogers

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

我想看看 .net 框架中是否有几个月的官方枚举。

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.

在我看来,有一个是可能的,因为月的使用非常普遍,并且因为 .net 框架中还有其他这样的枚举。

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

例如,一周中的天数有一个枚举 System.DayOfWeek,其中包括星期一、星期二等。

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

我想知道一年中的月份是否有一个,即一月、二月等?

Does anyone know?

有人知道吗?

采纳答案by Andy Mikula

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 GetMonthtakes arguments from 1 to 13 - January is 1, 13 is a blank string.

这将返回一个字符串表示(在本例中为当前月份)。请注意,GetMonth参数从 1 到 13 - 一月是 1,13 是一个空字符串。

回答by David Nelson

No, there isn't.

不,没有。

回答by Scott Wisniewski

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.

我不确定,但我的预感是不。DateTime.Month 返回一个整数。如果有这样的枚举,它可能会由 DateTime 返回。

回答by Charles Bretana

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

如果您只想将月份指定为某个其他对象类型的枚举属性,那么 DateTime 字段的 Month 属性将返回一个从 1 到 12 的整数...

回答by vidalsasoon

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

在此命名空间的枚举“MonthNamesType”中找到一个:Microsoft.ServiceModel.Channels.Mail.ExchangeWebService.Exchange2007

The location kinda scares but it's there nonetheless.

位置有点吓人,但它就在那里。

回答by Jadawin

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

里面有这个神奇的数字 1。然而

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

is self-documenting

是自我记录的

回答by Josh Stodola

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

是的,肯定有。它是 Microsoft.VisualBasic 命名空间的一部分...

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.

对于那些对此命名空间有问题的人,您应该明白它确实是 .NET,而且不会去任何地方

For the record, the MonthNamefunction internally calls the following...

为了记录,该MonthName函数在内部调用以下...

Thread.CurrentThread.CurrentCulture.DateTimeFormat.GetMonthName

回答by Francois Gagnon

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

有些日历确实有超过 12 个月的时间:http: //en.wikipedia.org/wiki/Month但我不能说这是否是 MS 没有在 .NET 中构建枚举的原因。

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

对于像我这样喜欢复制/粘贴的懒人,在 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

回答by Doug Lampe

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:

DateTimeFormatInfo.CurrentInfo.MonthNames(不是枚举,但我认为 DateTimeFormatInfo 的 CurrentInfo 实例通常是您正在寻找的)。如果你想要一个下拉列表,你可以像这样构建它:

List<string> monthNames = DateTimeFormatInfo.CurrentInfo.MonthNames.Take(12).ToList();
var monthSelectList = monthNames.Select(
   m => new { Id = monthNames.IndexOf(m) + 1, Name = m });

回答by wal

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

我想看看 .net 框架中是否有几个月的官方枚举。

No.

不。

Heres one I prepared earlier. (C# Version)

这是我之前准备的。(C# 版)

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
}