Skip to content

Instantly share code, notes, and snippets.

@pedroinfo
Created December 9, 2025 19:35
Show Gist options
  • Select an option

  • Save pedroinfo/0cb9c83fbca892f3f9ca5d146df10dd1 to your computer and use it in GitHub Desktop.

Select an option

Save pedroinfo/0cb9c83fbca892f3f9ca5d146df10dd1 to your computer and use it in GitHub Desktop.
TimeSpanFormatter
public static class TimeSpanFormatter
{
public static string ToReadableString(TimeSpan ts)
{
var parts = new List<string>();
if (ts.Days > 0)
parts.Add($"{ts.Days} day{(ts.Days > 1 ? "s" : "")}");
if (ts.Hours > 0)
parts.Add($"{ts.Hours} hour{(ts.Hours > 1 ? "s" : "")}");
if (ts.Minutes > 0)
parts.Add($"{ts.Minutes} minute{(ts.Minutes > 1 ? "s" : "")}");
if (ts.Seconds > 0)
parts.Add($"{ts.Seconds} second{(ts.Seconds > 1 ? "s" : "")}");
if (parts.Count == 0)
return "0 seconds";
return string.Join(", ", parts);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment