Get day, month, date, time, and number format information for the computer's locale in C#

The System.Globalization namespace's InstalledUICulture object provides lots of static methods giving information about the computer's numeric, date, and time formatting. This example adds a bunch of these values to a ListView control. The following code shows how the program adds just a few of the values.
private void Form1_Load(object sender, EventArgs e)
{
// Day/Month values.
AddHeader("Day/Month");
AddArrayItems("Day", CultureInfo.InstalledUICulture.DateTimeFormat.DayNames);
AddArrayItems("Abbrev Day", CultureInfo.InstalledUICulture.DateTimeFormat.AbbreviatedDayNames);
AddArrayItems("Short Days", CultureInfo.InstalledUICulture.DateTimeFormat.ShortestDayNames);
AddArrayItems("Month", CultureInfo.InstalledUICulture.DateTimeFormat.MonthNames);
AddArrayItems("Abbrev Month", CultureInfo.InstalledUICulture.DateTimeFormat.AbbreviatedMonthNames);

// Date/Time values.
AddHeader("Date/Time Format");
AddItem("AMDesignator", CultureInfo.InstalledUICulture.DateTimeFormat.AMDesignator);
AddItem("DateSeparator", CultureInfo.InstalledUICulture.DateTimeFormat.DateSeparator);
AddItem("FirstDayOfWeek", CultureInfo.InstalledUICulture.DateTimeFormat.FirstDayOfWeek.ToString());
AddItem("FullDateTimePattern", CultureInfo.InstalledUICulture.DateTimeFormat.FullDateTimePattern);
...

lvwValues.Columns[0].Width = -2;
lvwValues.Columns[1].Width = -2;
}

Helper routines add different kinds of values to the ListView control.

// Add a header row.
private void AddHeader(string name)
{
ListViewItem lvi = lvwValues.Items.Add(name);
lvi.BackColor = Color.Pink;
}

// Add a value to the result.
private void AddItem(string name, string value)
{
ListViewItem lvi = lvwValues.Items.Add(name);
lvi.SubItems.Add(value);
}

// Add all values in an array.
private void AddArrayItems(string name, string[] values)
{
for (int i = 0; i < values.Length; i++)
{
AddItem(name + "(" + i + ")", values[i]);
}
}

// Add all values in an integer array.
private void AddIntegerArrayItems(string name, int[] values)
{
for (int i = 0; i < values.Length; i++)
{
AddItem(name + "(" + i + ")", values[i].ToString());
}
}

  

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.