Friday, June 17, 2011

How to convert date time format based on the languages in C#.NET


How to convert date time format based on the languages in C#.NET

If you want to convert date into different languages date, you can use .NET globalization library for this purpose, see the example below

Namespace

using System.Globalization;

Source Code

//Type the ISO code based on languages you want like English: en-us, Genman: de, French (Canada): fr-ca 
CultureInfo culInfo = new CultureInfo(textBox1.Text);
//I am converting current date into desired language datetime format
DateTime dtUs = Convert.ToDateTime(DateTime.Now, culInfo.DateTimeFormat);
//this will give Typed language date format
label1.Content = string.Format("{0} {1}",culInfo.DateTimeFormat.ShortDatePattern,culInfo.DateTimeFormat.LongTimePattern);
//this will give Typed language date and time
label2.Content = dtUs.ToString(culInfo.DateTimeFormat.ShortDatePattern + " " + culInfo.DateTimeFormat.LongTimePattern);

Check code like

textBox1.Text  = “fr-ca”  // for Canada (French)

No comments: