Tuesday, October 5, 2010

Using the DateTimeFormatInfo to retrieve Culture Specific Date Values

This Blog entry discusses how to use the System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat object to access culture specific date values.

Retrieving Culture Specific Day Names
Access the DayNames property or AbbreviatedDayNames property to access a list of day names for the particular culture.
Example:

DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
foreach (string abbreviatedDayName in d.AbbreviatedDayNames)
Console.WriteLine (abbreviatedDayName);

Retrieving Culture Specific Month Names
Access the MonthNames property or AbbreviatedMonthNames property to access a list of month names for the particular culture.
Example:

DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;
foreach (string abbreviatedMonthName in d.AbbreviatedMonthNames)
Console.WriteLine (abbreviatedDayName);

Play with it:

This tutorial allows you to retrieve culture specific date values in French:

1.       Set the CurrentCulture to French (Canada)
Thread.CurrentThread.CurrentCulture = new CultureInfo("FR-CA");

2.       Declare a DateTimeFormatInfo and set it to the CurrentThread’s DateTimeFormat property
DateTimeFormatInfo d = Thread.CurrentThread.CurrentCulture.DateTimeFormat;           

3.       Write the day names for French (Canada)
foreach (string dayName in d.DayNames)
Console.WriteLine(dayName);

4.       Write the month names for French (Canada)
foreach (string monthName in d.MonthNames )
Console.WriteLine(monthName);

No comments:

Post a Comment