Get date format information for the computer’s locale in C#


The System.Globalization namespace’s InstalledUICulture object provides lots of static methods giving information about numeric, date, and time formatting for the computer’s locale. 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)
    // Save the culture (to make the following code shorter).
    CultureInfo info = CultureInfo.InstalledUICulture;

    // Day/Month values.
    AddArrayItems("Abbrev Day",
    AddArrayItems("Short Days",
    AddArrayItems("Abbrev Month",

    // Date/Time values.
    AddHeader("Date/Time Format:");

The following helper methods 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);

// 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());

The AddHeader method creates a ListViewItem with a pink background and no sub-items.

The AddItem method creates a ListViewItem holding a setting name and a sub-item holding the setting’s value.

The AddArrayItems method creates a header and then loops through a setting that is an array, calling AddItem to display the array’s items. If you look at the picture at the top of the post, you’ll see an unexpected blank value at the end of the list of month abbreviations. There’s a corresponding blank value at the end of the array of full month names.

Finally the AddIntegerArrayItems method works much as AddArrayItems does but it displays integer values.

You can use these values to determine how to format various values for the computer’s locale. Or better still, use locale-aware methods and format strings to display values in the computer’s locale automatically.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in internationalization, system and tagged , , , , , , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.