📜  在 C# 中使用月份编号获取月份名称

📅  最后修改于: 2022-05-13 01:56:12.112000             🧑  作者: Mango

在 C# 中使用月份编号获取月份名称

给定一个数字N (1<= N <=12) ,任务是C# 中的月份编号中获取月份名称。

例子:

Input: N = 2
Output: February
  
Input: N = 5
Output: May

方法 1:使用DateTime.ToString()方法:该方法可用于获取月份的完整名称和缩写名称。

下面是上述方法的实现:

C#
DateTime date = new DateTime(year, month, day);;



C#
// to get the Abbreviated month name 
string month_name = date.ToString("MMM");

// to get the full month name 
string month_name = date.ToString("MMMM");



C#
// C# program to Get a Month Name
// from a Month Number
using System;
   
public class GFG{
     
    // function to get the abbreviated month name
    static string getAbbreviatedName(int month) 
    {
        DateTime date = new DateTime(2020, month, 1);
         
        return date.ToString("MMM");
    }
     
    // function to get the full month name
    static string getFullName(int month) 
    {
        DateTime date = new DateTime(2020, month, 1);
         
        return date.ToString("MMMM");
    }
     
    static public void Main ()
    { 
        int N = 3; 
        Console.WriteLine("Full Month Name : " + getFullName(N));
         
        N = 7;
        Console.WriteLine("Abbreviated Month : " + getAbbreviatedName(N));
    } 
}


输出:

// C# program to Get a Month Name
// from a Month Number
using System;
using System.Globalization;
   
public class GFG{
     
    // function to get the full month name
    static string getFullName(int month) 
    {
        return CultureInfo.CurrentCulture.
            DateTimeFormat.GetMonthName
            (month);
    }
     
    static public void Main ()
    { 
        int N = 3; 
        Console.WriteLine("Full Month Name : " + getFullName(N));
         
        N = 7;
        Console.WriteLine("Full Month Month : " + getFullName(N));
    } 
}

方法二:使用GetMonthName()方法:该方法用于获取月份的全名。

下面是上述方法的实现:

C#

// C# program to Get a Month Name
// from a Month Number
using System;
using System.Globalization;
   
public class GFG{
     
    // function to get the Abbreviated month name
    static string getAbbreviatedName(int month) 
    {
        return CultureInfo.CurrentCulture.
            DateTimeFormat.GetAbbreviatedMonthName
            (month);
    }
     
    static public void Main ()
    { 
        int N = 3; 
        Console.WriteLine("Abbreviated Month Name : " + getAbbreviatedName(N));
         
        N = 7;
        Console.WriteLine("Abbreviated Month Month : " + getAbbreviatedName(N));
    } 
}

输出:

Full Month Name : March
Abbreviated Month : Jul

方法三:使用GetAbbreviatedMonthName ()方法:该方法用于获取月份的缩写名称。

下面是上述方法的实现:

C#

// to get the full month name
string monthName = CultureInfo.CurrentCulture.
DateTimeFormat.GetMonthName(month);

输出:

// C# program to Get a Month Name
// from a Month Number
using System;
using System.Globalization;
   
public class GFG{
     
    // function to get the full month name
    static string getFullName(int month) 
    {
        return CultureInfo.CurrentCulture.
            DateTimeFormat.GetMonthName
            (month);
    }
     
    static public void Main ()
    { 
        int N = 3; 
        Console.WriteLine("Full Month Name : " + getFullName(N));
         
        N = 7;
        Console.WriteLine("Full Month Month : " + getFullName(N));
    } 
}