C# DateTime Format

C# DateTime Format

using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("C# DateTime Format\n");
        DateTime now = DateTime.Now;

        Console.WriteLine(String.Format("{0:y yy yyy yyyy}", now));  // "8 08 008 2008"   year
        Console.WriteLine(String.Format("{0:M MM MMM MMMM}", now));  // "3 03 Mar March"  month
        Console.WriteLine(String.Format("{0:d dd ddd dddd}", now));  // "9 09 Sun Sunday" day
        Console.WriteLine(String.Format("{0:h hh H HH}", now));  // "4 04 16 16"      hour 12/24
        Console.WriteLine(String.Format("{0:m mm}", now));  // "5 05"            minute
        Console.WriteLine(String.Format("{0:s ss}", now));  // "7 07"            second
        Console.WriteLine(String.Format("{0:f ff fff ffff}", now));  // "1 12 123 1230"   sec.fraction
        Console.WriteLine(String.Format("{0:F FF FFF FFFF}", now));  // "1 12 123 123"    without zeroes
        Console.WriteLine(String.Format("{0:t tt}", now));  // "P PM"            A.M. or P.M.
        Console.WriteLine(String.Format("{0:z zz zzz}", now));  // "-6 -06 -06:00"   time zone

        // month/day numbers without/with leading zeroes
        Console.WriteLine(String.Format("{0:M/d/yyyy}", now));  // "3/9/2008"
        Console.WriteLine(String.Format("{0:MM/dd/yyyy}", now));  // "03/09/2008"

        // day/month names
        Console.WriteLine(String.Format("{0:ddd, MMM d, yyyy}", now));  // "Sun, Mar 9, 2008"
        Console.WriteLine(String.Format("{0:dddd, MMMM d, yyyy}", now));  // "Sunday, March 9, 2008"

        // two/four digit year
        Console.WriteLine(String.Format("{0:MM/dd/yy}", now));  // "03/09/08"
        Console.WriteLine(String.Format("{0:MM/dd/yyyy}", now));  // "03/09/2008"
        // Standard DateTime Formatting
        Console.WriteLine(String.Format("{0:t}", now));  // "4:05 PM"                           ShortTime
        Console.WriteLine(String.Format("{0:d}", now));  // "3/9/2008"                          ShortDate
        Console.WriteLine(String.Format("{0:T}", now));  // "4:05:07 PM"                        LongTime
        Console.WriteLine(String.Format("{0:D}", now));  // "Sunday, March 09, 2008"            LongDate
        Console.WriteLine(String.Format("{0:f}", now));  // "Sunday, March 09, 2008 4:05 PM"    LongDate+ShortTime
        Console.WriteLine(String.Format("{0:F}", now));  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
        Console.WriteLine(String.Format("{0:g}", now));  // "3/9/2008 4:05 PM"                  ShortDate+ShortTime
        Console.WriteLine(String.Format("{0:G}", now));  // "3/9/2008 4:05:07 PM"               ShortDate+LongTime
        Console.WriteLine(String.Format("{0:m}", now));  // "March 09"                          MonthDay
        Console.WriteLine(String.Format("{0:y}", now));  // "March, 2008"                       YearMonth
        Console.WriteLine(String.Format("{0:r}", now));  // "Sun, 09 Mar 2008 16:05:07 GMT"     RFC1123
        Console.WriteLine(String.Format("{0:s}", now));  // "2008-03-09T16:05:07"               SortableDateTime
        Console.WriteLine(String.Format("{0:u}", now));  // "2008-03-09 16:05:07Z"              UniversalSortableDateTime
    }
}
DEMO
C# DateTime Format C# DateTime Format Reviewed by Bhaumik Patel on 10:37 PM Rating: 5