andrew4582

TimeSpan.ToFormattedString

Jul 18th, 2011
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. public static string ToFormattedString(this TimeSpan value)
  2.         {
  3.             var list = new List<string>(3);
  4.  
  5.             int days = value.Days;
  6.             int hours = value.Hours;
  7.             int minutes = value.Minutes;
  8.             int seconds = value.Seconds;
  9.             int milliseconds = value.Milliseconds;
  10.  
  11.             if (days > 1)
  12.                 list.Add(String.Format("{0} days", days));
  13.             else if (days == 1)
  14.                 list.Add(String.Format("{0} day", days));
  15.  
  16.             if (hours > 1)
  17.                 list.Add(String.Format("{0} hours", hours));
  18.             else if (hours == 1)
  19.                 list.Add(String.Format("{0} hour", hours));
  20.  
  21.             if (minutes > 1)
  22.                 list.Add(String.Format("{0} minutes", minutes));
  23.             else if (minutes == 1)
  24.                 list.Add(String.Format("{0} minute", minutes));
  25.  
  26.             if (seconds > 1)
  27.                 list.Add(String.Format("{0} seconds", seconds));
  28.             else if (seconds == 1)
  29.                 list.Add(String.Format("{0} second", seconds));
  30.            
  31.             if (milliseconds > 1)
  32.                 list.Add(String.Format("{0} ms", milliseconds));
  33.             else if (milliseconds == 1)
  34.                 list.Add(String.Format("{0} ms", milliseconds));
  35.  
  36.             return String.Join(", ", list.ToArray());
  37.         }
Advertisement
Add Comment
Please, Sign In to add comment