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

[locale]

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.
    AddHeader("Day/Month:");
    AddArrayItems("Day",
        info.DateTimeFormat.DayNames);
    AddArrayItems("Abbrev Day",
        info.DateTimeFormat.AbbreviatedDayNames);
    AddArrayItems("Short Days",
        info.DateTimeFormat.ShortestDayNames);
    AddArrayItems("Month",
        info.DateTimeFormat.MonthNames);
    AddArrayItems("Abbrev Month",
        info.DateTimeFormat.AbbreviatedMonthNames);

    // Date/Time values.
    AddHeader("Date/Time Format:");
    AddItem("AMDesignator",
        info.DateTimeFormat.AMDesignator);
    AddItem("DateSeparator",
        info.DateTimeFormat.DateSeparator);
    AddItem("FirstDayOfWeek",
        info.DateTimeFormat.FirstDayOfWeek.ToString());
    AddItem("FullDateTimePattern",
        info.DateTimeFormat.FullDateTimePattern);
    ...
    lvwValues.AutoResizeColumns(
        ColumnHeaderAutoResizeStyle.ColumnContent);
}

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

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 *