This article explains how to get all month names of the year using C#.NET. First create an instance of the class DateTimeFormatInfo and get its CurrentInfo. There is property called MonthNames in the DateTimeFormatInfo which returs an array of month names.
Output sample:
January
February
March
April
May
June
July
August
September
October
November
December
using System.Globalization;
using System.Collections.Generic;
public ListItem[] GetMonthNamesList()
{
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
string[] _months = dfi.MonthNames;
List<ListItem> _monthNamesList = new List<ListItem>();
for (int i = 0; i <= 11; i++)
{
_monthNamesList.Add(new ListItem(_months[i], (i + 1).ToString()));
}
return _monthNamesList.ToArray();
}
using System.Collections.Generic;
public ListItem[] GetMonthNamesList()
{
DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
string[] _months = dfi.MonthNames;
List<ListItem> _monthNamesList = new List<ListItem>();
for (int i = 0; i <= 11; i++)
{
_monthNamesList.Add(new ListItem(_months[i], (i + 1).ToString()));
}
return _monthNamesList.ToArray();
}
Output sample:
January
February
March
April
May
June
July
August
September
October
November
December
No comments:
Post a Comment