没有标准方法,但您可以实现扩展方法:
public static partial class TimeSpanExtensions {
public static string ToMyFormat(this TimeSpan ts) {
return ts.Days != 0 ? $"{ts.Days} days"
: ts.Hours != 0 ? $"{ts.Hours} hours"
: ts.Minutes != 0 ? $"{ts.Minutes} minutes"
: ts.Seconds != 0 ? $"{ts.Seconds} seconds"
: $"{ts.Milliseconds} milliseconds";
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用它:
TimeSpan test = new TimeSpan(0, 415, 48, 44, 452);
// 17 days (415 = 17 * 24 + 7 - 17 days 7 hours)
Console.Write(ts.ToMyFormat());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
265 次 |
| 最近记录: |