using System; class Sample { public static void Main() { Console.WriteLine("(u) Universal sortable: . . . {0:u} (invariant) ",DateTime.Now); } }
Author: coder
Sortable date format
using System; class Sample { public static void Main() { Console.WriteLine("(s) Sortable: . . . . . . . . {0:s} ",DateTime.Now); } }
Long date format
using System; class Sample { public static void Main() { Console.WriteLine("(D) Long date:. . . . . . . . {0:D} ",DateTime.Now); } }
Format date value as short date format
using System; class Sample { public static void Main() { Console.WriteLine("(d) Short date: . . . . . . . {0:d} ",DateTime.Now); } }
Compare the results of choosing InvariantInfo with those of choosing CurrentInfo:
using System; using System.Globalization; public class MainClass { public static void Main() { DateTime dt = new DateTime(); DateTimeFormatInfo dtfi; Console.Write("[I]nvariant or [C]urrent Info?: "); if (Console.Read() == 'I') dtfi = DateTimeFormatInfo.InvariantInfo; else dtfi = DateTimeFormatInfo.CurrentInfo; dtfi = DateTimeFormatInfo.InvariantInfo; Console.WriteLine(dt.ToString("D", dtfi)); Console.WriteLine(dt.ToString("f", dtfi)); Console.WriteLine(dt.ToString("F", dtfi)); Console.WriteLine(dt.ToString("g", dtfi)); Console.WriteLine(dt.ToString("G", dtfi)); Console.WriteLine(dt.ToString("m", dtfi)); Console.WriteLine(dt.ToString("r", dtfi)); Console.WriteLine(dt.ToString("s", dtfi)); Console.WriteLine(dt.ToString("t", dtfi)); Console.WriteLine(dt.ToString("T", dtfi)); Console.WriteLine(dt.ToString("u", dtfi)); Console.WriteLine(dt.ToString("U", dtfi)); Console.WriteLine(dt.ToString("d", dtfi)); Console.WriteLine(dt.ToString("y", dtfi)); Console.WriteLine(dt.ToString("dd-MMM-yy", dtfi)); } }
Request a specific culture (english language in Great Britain):
using System; using System.Globalization; public class MainClass { public static void Main() { CultureInfo uk = CultureInfo.GetCultureInfo("en-GB"); Console.WriteLine(3.ToString("C", uk)); // ?¡ê3.00 } }
The next example formats a DateTime with invariant culture. Invariant culture is always the same, regardless of the computer's settings:
using System; using System.Globalization; public class MainClass { public static void Main() { DateTime dt = new DateTime(2000, 1, 2); CultureInfo iv = CultureInfo.InvariantCulture; Console.WriteLine(dt.ToString(iv)); // 01/02/2000 00:00:00 Console.WriteLine(dt.ToString("d", iv)); // 01/02/2000 } }