Get Month name from month number
C#.NetDatetimeC# Problem Overview
> Possible Duplicate:
> How to get the MonthName in c#?
I used the following c# syntax to get month name from month no but i get August
i want only Aug
..
System.Globalization.DateTimeFormatInfo mfi = new
System.Globalization.DateTimeFormatInfo();
string strMonthName = mfi.GetMonthName(8).ToString();
Any suggestion...
C# Solutions
Solution 1 - C#
For short month names use:
string monthName = new DateTime(2010, 8, 1)
.ToString("MMM", CultureInfo.InvariantCulture);
For long/full month names for Spanish ("es") culture
string fullMonthName = new DateTime(2015, i, 1).ToString("MMMM", CultureInfo.CreateSpecificCulture("es"));
Solution 2 - C#
For Abbreviated Month Names : "Aug"
DateTimeFormatInfo.GetAbbreviatedMonthName Method (Int32)
> Returns the culture-specific abbreviated name of the specified month > based on the culture associated with the current DateTimeFormatInfo > object.
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(8)
For Full Month Names : "August"
DateTimeFormatInfo.GetMonthName Method (Int32)
> Returns the culture-specific full name of the specified month based on > the culture associated with the current DateTimeFormatInfo object.
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(8);
Solution 3 - C#
Replace GetMonthName
with GetAbbreviatedMonthName
so that it reads:
string strMonthName = mfi.GetAbbreviatedMonthName(8);
Solution 4 - C#
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(4)
This method return April
If you need some special language, you can add:
<system.web>
<globalization culture="es-ES" uiCulture="es-ES"></globalization>
<compilation debug="true"
</system.web>
Or your preferred language.
For example, with es-ES
culture:
System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(4)
Returns: Abril
Returns: Abril
(in spanish, because, we configured the culture as es-ES
in our webconfig
file, else, you will get April
)
That should work.
Solution 5 - C#
You can get this in following way,
DateTimeFormatInfo mfi = new DateTimeFormatInfo();
string strMonthName = mfi.GetMonthName(8).ToString(); //August
Now, get first three characters
string shortMonthName = strMonthName.Substring(0, 3); //Aug
Solution 6 - C#
You want GetAbbreviatedMonthName
Solution 7 - C#
This should return month text (January - December) from the month index (1-12)
int monthNumber = 1; //1-12
string monthName = new DateTimeFormatInfo().GetMonthName(monthNumber);
Solution 8 - C#
You can also do this to get current Month :
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month);
Solution 9 - C#
var month = 5;
var cultureSwe = "sv-SE";
var monthSwe = CultureInfo.CreateSpecificCulture(cultureSwe).DateTimeFormat.GetAbbreviatedMonthName(month);
Console.WriteLine(monthSwe);
var cultureEn = "en-US";
var monthEn = CultureInfo.CreateSpecificCulture(cultureEn).DateTimeFormat.GetAbbreviatedMonthName(month);
Console.WriteLine(monthEn);
Output
maj
may