C# 通用日期短時間 (“g”) 格式說明符


生成日期短時間格式說明符是指短日期(“d”)和短時間(“t”)模式的組合,用空格分隔。

使用 DateTime 設定日期。

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

現在,使用(“g”)格式說明符。

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

示例

 即時演示

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

輸出

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

更新於:23-06-2020

338 次瀏覽

開啟您的 職業生涯

完成課程並獲得認證

入門
廣告
© . All rights reserved.