Implementing globalization, drawing, and text manipulation functionality in a .NET Framework application section, we see
- Format date and time values based on the culture
The .NET framework makes this task pretty straight forward with the System.Globalization namespace. Within this name space there is the CultureInfo class. The CultureInfo class contains all of the specific formatting rules for a specific culture. Cultures are divided up by language and region.
So to exercise this objective we print out a date in five different culture specific formats. We pick the date December 21, 2012, the end of the Mayan calendar as a fun date to use. Links are provided below about this date if you want to see what all of the buzz is about. But onto the code sample
Date Formatted Based on Culture Example
using System;
using System.Globalization;
namespace DateTimeFormatExample
{
class Program
{
static void Main(string[] args)
{
//This example is going to show how to format
// a date for several cultures. We could pick
// any date for this example, but just for kicks
// we'll pick December 21, 2012. This date is
// mysterious to astrologists and conspiracy
// theorists because it is the end of the Mayan
// calandar... will it be THE end? Who knows, but
// we'll use it here for our example.
// Declare the date December 21, 2012
DateTime dt = new DateTime(2012, 12, 21, 4, 0, 0);
//First let's print out for US English
CultureInfo en_us_ci = new CultureInfo("en-US");
//Print short date format
Console.WriteLine("US English " + dt.ToString("d", en_us_ci));
//Next let's print out for Chinese, PRC
CultureInfo zh_cn_ci = new CultureInfo("zh-CN");
Console.WriteLine("Chinese, PRC " + dt.ToString("d", zh_cn_ci));
//Next let's print out for German (Germany)
CultureInfo de_de_ci = new CultureInfo("de-DE");
Console.WriteLine("German (Germany) " + dt.ToString("d", de_de_ci));
//Next let's print out for Hebrew (Israel)
CultureInfo he_il_ci = new CultureInfo("he-IL");
Console.WriteLine("Hebrew (Israel) " + dt.ToString("d", he_il_ci));
//Next let's print out for Punjabi (India)
CultureInfo pa_in_ci = new CultureInfo("pa-IN");
Console.WriteLine("Punjabi (India) " + dt.ToString("d", pa_in_ci));
}
}
}
Date Format Example Output
US English 12/21/2012
Chinese, PRC 2012/12/21
German (Germany) 21.12.2012
Hebrew (Israel) 21/12/2012
Punjabi (India) 21-12-12
Additional Resources
CultureInfo Class (Microsoft)
Non code related...
2012 (Wikipedia)
Survive 2012