C# General Date Short Time ("g") Format Specifier

CsharpProgrammingServer Side Programming

The Generate Date Short Time format specifier is a combination of the short date ("d") and short time ("t") patterns, separated by a space.

Set a date using DateTime.

DateTime dt = new DateTime(2018, 10, 2, 7, 59, 20);

Now, use the (“g”) format specifier.

dt.ToString("g", DateTimeFormatInfo.InvariantInfo));

Example

 Live Demo

using System;
using System.Globalization;
class Demo {
   static void Main() {
      DateTime dt = new DateTime(2018, 10, 2, 7, 59, 20);
      Console.WriteLine(dt.ToString("g", DateTimeFormatInfo.InvariantInfo));
      Console.WriteLine(dt.ToString("g", CultureInfo.CreateSpecificCulture("en-us")));
   }
}

Output

10/02/2018 07:59
10/2/2018 7:59 AM
raja
Published on 12-Sep-2018 12:14:30
Advertisements