Format time and date information. : DateTime Format « Date Time « C# / CSharp Tutorial






using System;  
  
class MainClass {  
  public static void Main() {  
    DateTime dt = DateTime.Now; // obtain current time 
 
    Console.WriteLine("d format: {0:d}", dt); 
    Console.WriteLine("D format: {0:D}", dt); 
 
    Console.WriteLine("t format: {0:t}", dt); 
    Console.WriteLine("T format: {0:T}", dt); 
 
    Console.WriteLine("f format: {0:f}", dt); 
    Console.WriteLine("F format: {0:F}", dt); 
 
    Console.WriteLine("g format: {0:g}", dt); 
    Console.WriteLine("G format: {0:G}", dt); 
 
    Console.WriteLine("m format: {0:m}", dt); 
    Console.WriteLine("M format: {0:M}", dt); 
 
    Console.WriteLine("r format: {0:r}", dt); 
    Console.WriteLine("R format: {0:R}", dt); 
 
    Console.WriteLine("s format: {0:s}", dt); 
 
    Console.WriteLine("u format: {0:u}", dt); 
    Console.WriteLine("U format: {0:U}", dt); 
 
    Console.WriteLine("y format: {0:y}", dt); 
    Console.WriteLine("Y format: {0:Y}", dt); 
  } 
}
d format: 25/03/2007
D format: March 25, 2007
t format: 1:53 PM
T format: 1:53:55 PM
f format: March 25, 2007 1:53 PM
F format: March 25, 2007 1:53:55 PM
g format: 25/03/2007 1:53 PM
G format: 25/03/2007 1:53:55 PM
m format: March 25
M format: March 25
r format: Sun, 25 Mar 2007 13:53:55 GMT
R format: Sun, 25 Mar 2007 13:53:55 GMT
s format: 2007-03-25T13:53:55
u format: 2007-03-25 13:53:55Z
U format: March 25, 2007 8:53:55 PM
y format: March, 2007
Y format: March, 2007








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