Localize Date and Time through Specific Culture

The DateTime structure provides methods that allow your applications to perform culture-sensitive operations on a DateTime type. An application can use the DateTimeFormatInfo class to format and display a DateTime type based on culture.

Following sample code displays the date and time in different cultures are as follows,

public void DateFormatDisplay()
{

//To get the Current culture information
Console.WriteLine(Thread.CurrentThread.CurrentCulture);
DateTime dt = DateTime.Now;
// Creates a CultureInfo for English in US.
CultureInfo CulInf = new CultureInfo("en-US");
Console.WriteLine(dt.ToString("d"));
// Creates a CultureInfo for German in Germany.
CulInf = new CultureInfo("de-DE");
Console.WriteLine(dt.ToString("d", CulInf));

// Creates a CultureInfo for English in Great Britan.
CulInf = new CultureInfo("en-GB");
Console.WriteLine(dt.ToString("d", CulInf));
}

If Current date is 14-Apr-2009 then output of above coding are as follows,

en-US
4/14/2009
14.04.2009
14/04/2009

No comments: