Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Định dạng General Date Short Time (g) trong C#

Chuẩn "g" (General Date Short Time) kết hợp Short Date ("d") và Short Time ("t"), cách nhau bởi dấu cách. Mẫu Invariant: MM/dd/yyyy HH:mm.

Ví dụ minh họa

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")));
    }
}

Kết quả đầu ra

10/02/2018 07:59
10/2/2018 7:59 AM

So sánh:

  • g: Short Date + Short Time
  • G: Short Date + Long Time (có giây)
  • f: Long Date + Short Time
  • F: Long Date + Long Time