[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Get date format information for the computer's locale in C#

[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. 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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.