Computer >> Computer tutorials >  >> Programming >> C#

General Date Long Time (G) Format Specifier in C#


The General Date Long Time standard format specifier is a combination of the short date ("d") and long time ("T") patterns, separated by a space.

Set the date −

DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);

Now, use the ToString() method and DateTimeFormatInfo.

dt.ToString("G", DateTimeFormatInfo.InvariantInfo)

Example

using System;
using System.Globalization;
class Demo {
   static void Main() {
      DateTime dt = new DateTime(2018, 1, 3, 3, 45, 20);
      Console.WriteLine(dt.ToString("G",
      DateTimeFormatInfo.InvariantInfo));
   }
}

Output

01/03/2018 03:45:20