using System; using System.Text; namespace JD.Core { public static class TimeSpanExtensions { public static string ToReadableString(this TimeSpan _this) { var time = new StringBuilder(); var needSpace = false; if (_this.Days > 0) { time.AppendFormat("{0}d ", _this.Days); needSpace = true; } if (_this.Hours > 0) { time.AppendFormat(needSpace ? " {0}h" : "{0}h", _this.Hours); needSpace = true; } if (_this.Minutes > 0) { time.AppendFormat(needSpace ? " {0}m" : "{0}m", _this.Minutes); needSpace = true; } if (_this.Seconds > 0) { time.AppendFormat(needSpace ? " {0}s" : "{0}s", _this.Seconds); needSpace = true; } time.AppendFormat(needSpace ? " {0}ms" : "{0}ms", _this.Milliseconds); return time.ToString(); } } }