Created
December 9, 2025 19:35
-
-
Save pedroinfo/0cb9c83fbca892f3f9ca5d146df10dd1 to your computer and use it in GitHub Desktop.
TimeSpanFormatter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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