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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 23-Jun-2020

286 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements