Created
October 24, 2013 15:56
-
-
Save umair-me/7139854 to your computer and use it in GitHub Desktop.
convert timespan to readable string format
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 string ToReadableString(this TimeSpan span) | |
{ | |
string formatted = string.Format("{0}{1}{2}{3}", | |
span.Duration().Days > 0 ? string.Format("{0:0} day{1}, ", span.Days, span.Days == 1 ? String.Empty : "s") : string.Empty, | |
span.Duration().Hours > 0 ? string.Format("{0:0} hour{1}, ", span.Hours, span.Hours == 1 ? String.Empty : "s") : string.Empty, | |
span.Duration().Minutes > 0 ? string.Format("{0:0} minute{1}, ", span.Minutes, span.Minutes == 1 ? String.Empty : "s") : string.Empty, | |
span.Duration().Seconds > 0 ? string.Format("{0:0} second{1}", span.Seconds, span.Seconds == 1 ? String.Empty : "s") : string.Empty); | |
if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2); | |
if (string.IsNullOrEmpty(formatted)) formatted = "0 seconds"; | |
return formatted; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment