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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/975531/
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-06 04:40:11  来源:igfitidea点击:

How to get the month name in C#?

c#datetime

提问by

How does one go about finding the month name in C#? I don't want to write a huge switchstatement or ifstatement on the month int. In VB.Net you can use MonthName(), but what about C#?

如何在 C# 中找到月份名称?我不想在这个月写一个巨大的switch声明或if声明int。在 VB.Net 中你可以使用MonthName(),但是 C# 呢?

采纳答案by CodeLikeBeaker

You can use the CultureInfo to get the month name. You can even get the short month name as well as other fun things.

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

I would suggestion you put these into extension methods, which will allow you to write less code later. However you can implement however you like.

我建议您将这些放入扩展方法中,这将使您以后编写更少的代码。但是,您可以随心所欲地实施。

Here is an example of how to do it using extension methods:

以下是如何使用扩展方法进行操作的示例:

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

Hope this helps!

希望这可以帮助!

回答by Jon Skeet

Use the "MMMM" format specifier:

使用“MMMM”格式说明符:

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

回答by George Stocker

string CurrentMonth = String.Format("{0:MMMM}", DateTime.Now)

回答by RobV

If you just want to use MonthName then reference Microsoft.VisualBasic and it's in Microsoft.VisualBasic.DateAndTime

如果您只想使用 MonthName 然后引用 Microsoft.VisualBasic 并且它在 Microsoft.VisualBasic.DateAndTime 中

//eg. Get January
String monthName = Microsoft.VisualBasic.DateAndTime.MonthName(1);

回答by Binamra

Supposing your date is today. Hope this helps you.

假设你的日期是今天。希望这对你有帮助。

DateTime dt = DateTime.Today;

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

Console.WriteLine(thisMonth);

回答by Angel Alvarado Sagel

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