C# 中通用日期長時制 (“G”) 格式說明符


通用日期長時制的標準格式說明符是短日期 (“d”) 和長時制 (“T”) 的組合模式,由空格隔開。

設定日期 −

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

現在,使用 ToString() 方法和 DateTimeFormatInfo。

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

示例

 即時演示

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

輸出

01/03/2018 03:45:20

更新於: 2020 年 6 月 23 日

195 次訪問

開啟你的 職業生涯

完成課程即可獲得證書

開始學習
廣告
© . All rights reserved.