Collect day and month names of a culture in C#

The CultureInfo class helps you get the names of days and months in various languages that belong to the selected culture.

Here come 2 simple functions to collect the day and month names in string lists:

public List<string> GetWeekDaysByCulture(CultureInfo culture)
{
	return new List<string>
	{
									
		culture.DateTimeFormat.GetDayName(DayOfWeek.Monday), 
		culture.DateTimeFormat.GetDayName(DayOfWeek.Tuesday), 
		culture.DateTimeFormat.GetDayName(DayOfWeek.Wednesday), 
		culture.DateTimeFormat.GetDayName(DayOfWeek.Thursday), 
		culture.DateTimeFormat.GetDayName(DayOfWeek.Friday), 
		culture.DateTimeFormat.GetDayName(DayOfWeek.Saturday), 
		culture.DateTimeFormat.GetDayName(DayOfWeek.Sunday)
	};
}

public List<string> GetMonthNamesByCulture(CultureInfo culture)
{
	return new List<string>
	{
		culture.DateTimeFormat.GetMonthName(1),
		culture.DateTimeFormat.GetMonthName(2),
		culture.DateTimeFormat.GetMonthName(3),
		culture.DateTimeFormat.GetMonthName(4),
		culture.DateTimeFormat.GetMonthName(5),
		culture.DateTimeFormat.GetMonthName(6),
		culture.DateTimeFormat.GetMonthName(7),
		culture.DateTimeFormat.GetMonthName(8),
		culture.DateTimeFormat.GetMonthName(9),
		culture.DateTimeFormat.GetMonthName(10),
		culture.DateTimeFormat.GetMonthName(11),
		culture.DateTimeFormat.GetMonthName(12),
	};
}

You can call these functions as follows to get the French day and month names:

List<string> days = GetWeekDaysByCulture(new CultureInfo("fr-FR"));
List<string> months = GetMonthNamesByCulture(new CultureInfo("fr-FR"));

days.ForEach(d => Debug.WriteLine(d));
Debug.WriteLine("=====================");
months.ForEach(m => Debug.WriteLine(m));

Here’s the output:

lundi
mardi
mercredi
jeudi
vendredi
samedi
dimanche
=====================
janvier
février
mars
avril
mai
juin
juillet
août
septembre
octobre
novembre
décembre

Now you know the day and month names in French.

Read all posts related to Globalisation in .NET here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Collect day and month names of a culture in C#

  1. rachel says:

    you can use also. culture.DateTimeFormat.MonthNames it gives all the months

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: