Convert DateTime to string using specified culture-specific format in CSharp
Description
The following code shows how to convert DateTime to string using specified culture-specific format.
Example
using System;//www . j av a 2 s .c o m
using System.Globalization;
public class Example
{
public static void Main()
{
CultureInfo jaJP = new CultureInfo("ja-JP");
jaJP.DateTimeFormat.Calendar = new JapaneseCalendar();
DateTime date1 = new DateTime(1867, 1, 1);
try {
Console.WriteLine(date1.ToString(jaJP));
}
catch (ArgumentOutOfRangeException) {
Console.WriteLine("{0:d} is earlier than {1:d} or later than {2:d}",
date1,
jaJP.DateTimeFormat.Calendar.MinSupportedDateTime,
jaJP.DateTimeFormat.Calendar.MaxSupportedDateTime);
}
CultureInfo[] cultures = new CultureInfo[] {CultureInfo.InvariantCulture,
new CultureInfo("en-us"),
new CultureInfo("fr-fr"),
new CultureInfo("de-DE"),
new CultureInfo("es-ES"),
new CultureInfo("ja-JP")};
DateTime thisDate = new DateTime(2014, 5, 1, 9, 0, 0);
foreach (CultureInfo culture in cultures)
{
string cultureName;
if (string.IsNullOrEmpty(culture.Name))
cultureName = culture.NativeName;
else
cultureName = culture.Name;
Console.WriteLine("In {0}, {1}",
cultureName, thisDate.ToString(culture));
}
}
}
The code above generates the following result.