Use the ToString() method to convert a DateTime to a string: d, D, f, F, g, G, m ,r, s, t,T, u, U, y : DateTime Format « Date Time « C# / CSharp Tutorial






using System;

class MainClass
{
  public static void Main()
  {
    DateTime myDateTime = new DateTime(2004, 1, 12, 22, 2, 10);
    Console.WriteLine("myDateTime.ToString() = " + myDateTime.ToString());
    Console.WriteLine("myDateTime.ToString(\"d\") = " + myDateTime.ToString("d"));
    Console.WriteLine("myDateTime.ToString(\"D\") = " + myDateTime.ToString("D"));
    Console.WriteLine("myDateTime.ToString(\"f\") = " + myDateTime.ToString("f"));
    Console.WriteLine("myDateTime.ToString(\"F\") = " + myDateTime.ToString("F"));
    Console.WriteLine("myDateTime.ToString(\"g\") = " + myDateTime.ToString("g"));
    Console.WriteLine("myDateTime.ToString(\"G\") = " + myDateTime.ToString("G"));
    Console.WriteLine("myDateTime.ToString(\"m\") = " + myDateTime.ToString("m"));
    Console.WriteLine("myDateTime.ToString(\"r\") = " + myDateTime.ToString("r"));
    Console.WriteLine("myDateTime.ToString(\"s\") = " + myDateTime.ToString("s"));
    Console.WriteLine("myDateTime.ToString(\"t\") = " + myDateTime.ToString("t"));
    Console.WriteLine("myDateTime.ToString(\"T\") = " + myDateTime.ToString("T"));
    Console.WriteLine("myDateTime.ToString(\"u\") = " + myDateTime.ToString("u"));
    Console.WriteLine("myDateTime.ToString(\"U\") = " + myDateTime.ToString("U"));
    Console.WriteLine("myDateTime.ToString(\"y\") = " + myDateTime.ToString("y"));

  }
}
myDateTime.ToString() = 12/01/2004 10:02:10 PM
myDateTime.ToString("d") = 12/01/2004
myDateTime.ToString("D") = January 12, 2004
myDateTime.ToString("f") = January 12, 2004 10:02 PM
myDateTime.ToString("F") = January 12, 2004 10:02:10 PM
myDateTime.ToString("g") = 12/01/2004 10:02 PM
myDateTime.ToString("G") = 12/01/2004 10:02:10 PM
myDateTime.ToString("m") = January 12
myDateTime.ToString("r") = Mon, 12 Jan 2004 22:02:10 GMT
myDateTime.ToString("s") = 2004-01-12T22:02:10
myDateTime.ToString("t") = 10:02 PM
myDateTime.ToString("T") = 10:02:10 PM
myDateTime.ToString("u") = 2004-01-12 22:02:10Z
myDateTime.ToString("U") = January 13, 2004 6:02:10 AM
myDateTime.ToString("y") = January, 2004








13.3.DateTime Format
13.3.1.DateTime Formatting
13.3.2.Format time and date: {0:hh:mm tt}
13.3.3.Format time and date: 24 hour time is {0:HH:mm}
13.3.4.Format time and date: Date is {0:ddd MMM dd, yyyy}
13.3.5.Format time and date: Date is {0:gg}
13.3.6.Format time and date: Time with seconds{0:HH:mm:ss tt}
13.3.7.Format time and date: Use m for day of month{0:m}
13.3.8.Format time and date: use m for minutes: {0:%m}
13.3.9.Format time and date information.
13.3.10.Use the ToLongDateString() and ToShortDateString() methods to convert the date parts of a DateTime to long and short date strings
13.3.11.Use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
13.3.12.Use the ToString() method to convert a DateTime to a string: MMMM dd, yyyy
13.3.13.Use the ToString() method to convert a DateTime to a string: d, D, f, F, g, G, m ,r, s, t,T, u, U, y
13.3.14.Pre-built date/time specifiers
13.3.15.Culture-insensitive DateTime format strings
13.3.16.Culture-sensitive DateTime format strings
13.3.17.Format DateTime for different CultureInfo
13.3.18.DateTime long date pattern
13.3.19.Format DateTime with %M
13.3.20.DateTime custime format: MMMM dd, yyyy (dddd)
13.3.21.Format DayOfWeek