Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中获取月份名称?_C#_Datetime - Fatal编程技术网

如何在C#中获取月份名称?

如何在C#中获取月份名称?,c#,datetime,C#,Datetime,如何在C#中查找月份名称?我不想在int这个月写一个巨大的开关语句或if语句。在VB.Net中,您可以使用MonthName(),但是关于C#?如何使用“MMMM”格式说明符: string month = dateTime.ToString("MMMM"); 您可以使用CultureInfo获取月份名称。你甚至可以得到短月份的名字以及其他有趣的东西 我建议您将这些放在扩展方法中,这样以后就可以编写更少的代码。但是,您可以根据自己的喜好实施 下面是如何使用扩展方法执行此操作的示例: using

如何在C#中查找月份名称?我不想在
int
这个月写一个巨大的
开关
语句或
if
语句。在VB.Net中,您可以使用
MonthName()
,但是关于C#?

如何使用“MMMM”格式说明符:

string month = dateTime.ToString("MMMM");

您可以使用CultureInfo获取月份名称。你甚至可以得到短月份的名字以及其他有趣的东西

我建议您将这些放在扩展方法中,这样以后就可以编写更少的代码。但是,您可以根据自己的喜好实施

下面是如何使用扩展方法执行此操作的示例:

using System;
using System.Globalization;

class Program
{
    static void Main()
    {

        Console.WriteLine(DateTime.Now.ToMonthName());
        Console.WriteLine(DateTime.Now.ToShortMonthName());
        Console.Read();
    }
}

static class DateTimeExtensions
{
    public static string ToMonthName(this DateTime dateTime)
    {
        return CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(dateTime.Month);
    }

    public static string ToShortMonthName(this DateTime dateTime)
    {
        return CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(dateTime.Month);
    }
}

希望这有帮助

如果您只想使用MonthName,请参考Microsoft.VisualBasic,它位于Microsoft.VisualBasic.DateAndTime中

string CurrentMonth = String.Format("{0:MMMM}", DateTime.Now)
//eg. Get January
String monthName = Microsoft.VisualBasic.DateAndTime.MonthName(1);

假设你的约会是今天。希望这对你有帮助

DateTime dt = DateTime.Today;

string thisMonth= dt.ToString("MMMM");

Console.WriteLine(thisMonth);

实际上,您不需要ToString()或String.Format。只是DateTime.Now.ToString(“MMMM”)更简单。我也会建议这样做,但你已经有了另一个答案。Gortok的方法还演示了使用占位符进行格式化,因此我认为这是一个很好的替代示例。如果不是:
var month=newdatetime(1,i,1).ToString(“MMMM”)
我可以补充一点,也可以使用
InvariantInfo
属性。在我看来,以下是一种更简单/可读的格式:
DateTimeFormatInfo.InvariantInfo.get缩写月名(…)
DateTimeFormatInfo.CurrentInfo.get缩写月名(…)
这并没有回答OP的问题,因为他们说:“我真的不想写一个巨大的switch语句[…](强调我的)。在写答案之前,请仔细阅读问题。可能是重复的
    private string MonthName(int m)
    {
        string res;
        switch (m)
        {
            case 1:
                res="Ene";
                break;
            case 2:
                res = "Feb";
                break;
            case 3:
                res = "Mar";
                break;
            case 4:
                res = "Abr";
                break;
            case 5:
                res = "May";
                break;
            case 6:
                res = "Jun";
                break;
            case 7:
                res = "Jul";
                break;
            case 8:
                res = "Ago";
                break;
            case 9:
                res = "Sep";
                break;
            case 10:
                res = "Oct";
                break;
            case 11:
                res = "Nov";
                break;
            case 12:
                res = "Dic";
                break;
            default:
                res = "Nulo";
                break;
        }
        return res;
    }