You can display data and time in different ways with the help of DateTime format specifiers. Listing 3.4 explains each one of these specifiers in detail. I have given necessary explanations in the form of comments.
using System;
class DateTimeFormat
{
public static void Main()
{
DateTime dt = DateTime.Now;
// Returns Short Date (MM/DD/YYYY)
Console.WriteLine("Format Specifier (d): {0:d}",dt);
// Returns Long Date (Day, Month Date, Year)
Console.WriteLine("Format Specifier (D): {0:D}",dt);
// Returns Full Date with Time and without seconds
Console.WriteLine("Format Specifier (f):{0:f}",dt);
// Returns Full Date with Time and with Seconds
Console.WriteLine("Format Specifier (F):{0:F}",dt);
// Returns short date and short time without seconds
Console.WriteLine("Format Specifier (g):{0:g}",dt);
// Returns short date and short time with seconds
Console.WriteLine("Format Specifier (G):{0:G}",dt);
// Returns Month and Day - M can also be used
Console.WriteLine("Format Specifier (m):{0:m}",dt);
// Returns the date and time in RFC1123 format
// r can also be used
Console.WriteLine("Format Specifier (R):{0:R}",dt);
// Returns Short Time
Console.WriteLine("Format Specifier (t):{0:t}",dt);
// Returns Short Time with Second
Console.WriteLine("Format Specifier (T):{0:T}",dt);
// Returns Year and Month - Y also can be used
Console.WriteLine("Format Specifier (y):{0:y}",dt);
}
}
The final output of the above code will look like Figure 3.4.