在 C# 中格式化 TimeSpan

csharpprogrammingserver side programming更新于 2025/5/23 18:37:17

您可以在 C# 中以 hh:mm:ss 格式格式化 TimeSpan。

首先,设置 TimeSpan −

TimeSpan ts = new TimeSpan(9, 15, 30);

格式化 TimeSpan −

{0:hh\:mm\:ss}

以下是代码 −

示例

using System;
using System.Linq;
public class Demo {
   public static void Main() {
      TimeSpan ts = new TimeSpan(9, 15, 30);
      Console.WriteLine("{0:hh\:mm\:ss}", ts);
   }
}

输出

09:15:30

相关文章