C# Date Time Format Strings

There are several format strings available in C# to convert the DateTime into a string. In this article, we discuss how we can apply each format string on date time. Open Microsoft Visual Studio => Create a new console application and name it as CSharpDateTimeFormat. Declare a date time variable as shown below.

using System; 

namespace CSharpDateTimeFormat

{

    class Program

    {

        static void Main(string[] args)

        {

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

        }

    }

} 

As shown we declare the variable date1 as 12/20/2016 10:34:37. Here we discuss how can convert this datetime variable into different string formats.

Short Date: By applying format string “d”, we can convert datetime field into simple date format as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("d"));

            Console.ReadLine();

The above code displays the output as 12/20/2016 that means only date value from datetime field.

Long Date: To display date with week name and month name, apply format string “D” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("D"));

            Console.ReadLine();

It displays the output as Tuesday, December 20, 2016.

Short Time: To display the time in hh:mm format, apply the format string “t” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("t"));

            Console.ReadLine();

It displays the output as 10:34 AM.

Long Time: To view the time in hh:mm:ss format, apply the format string “T” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("T"));

            Console.ReadLine();

It displays the output as 10:34:37 AM.

Long Date & Short Time: To see the long date with short time, apply string format “f” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("f"));

            Console.ReadLine();

It displays the output as Tuesday, December 20, 2016 10:34 AM.

Long Date & Long Time: To display the long date with a long time, apply string format “F” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("F"));

            Console.ReadLine();

It displays the output as Tuesday, December 20, 2016 10:34:37 AM.

Short Date & Short Time: To view the short date with short time, apply string format “F” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("g"));

            Console.ReadLine();

It displays the output as 12/20/2016 10:34 AM.

Short Date & Long Time: To display the short date with short time, apply string format “F” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("G"));

            Console.ReadLine();

It displays the output as 12/20/2016 10:34:37 AM. This is default format that means if we don’t mention any format by default this format get applies.

Month OR Day: To display month and date, apply string format “m” or “M” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("M"));

            Console.ReadLine();

It displays the output as December 20.

Year OR Month: To display year and month, apply string format “y” or “Y” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("Y"));

            Console.ReadLine();

It displays the output as December 2016.

UTC: To display UTC time in Long date and short time format, apply string format “U” as shown below.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("U"));

            Console.ReadLine();

It displays the output as Tuesday, December 20, 2016 5:04:37 AM.

RoundTrippable: By applying string format “o” it displays the output as “2016-12-20T10:34:37.0000000”.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("o"));

            Console.ReadLine();

RFC 1123 standard: By applying string format “r” or “R” it displays the output as “Tue, 20 Dec 2016 10:34:37 GMT”.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("R"));

            Console.ReadLine();

Sortable;ISO 8601: By applying string format “s” it displays the output as “2016-12-20T10:34:37”.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("s"));

            Console.ReadLine();

"Universal" sortable: By applying string format “s” it displays the output as “2016-12-20 10:34:37Z”.

            DateTime date1= new DateTime(2016, 12, 20, 10, 34, 37);

            Console.WriteLine(date1.ToString("u"));

            Console.ReadLine();