在 C# 中使用月份编号获取月份名称
给定一个数字N (1<= N <=12) ,任务是从C# 中的月份编号中获取月份名称。
例子:
Input: N = 2
Output: February
Input: N = 5
Output: May
方法 1:使用DateTime.ToString()方法:该方法可用于获取月份的完整名称和缩写名称。
Step 1: Get the Number N.
Step 2: Create an object of DateTime using the DateTime Struct.
Step 3: Converts the value of the current DateTime object to its equivalent string representation.
Step 4: This string contains the name of the month.
下面是上述方法的实现:
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()方法:该方法用于获取月份的全名。
Step 1: Get the Number N.
Step 2: Create an object of CultureInfo and then get the month name using DateTimeFormat.GetMonthName() method.
Step 3: This string contains the name of the month.
下面是上述方法的实现:
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 ()方法:该方法用于获取月份的缩写名称。
Step 1: Get the Number N.
Step 2: Create an object of CultureInfo and then get the month name using DateTimeFormat.GetAbbreviatedMonthName() method.
// to get the Abbreviated month name
string monthName = CultureInfo.CurrentCulture.
DateTimeFormat.GetAbbreviatedMonthName(month);
Step 3: This string contains the name of the month.
下面是上述方法的实现:
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));
}
}