Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First you need to convert it to a <code>TimeSpan</code>:</p> <pre><code>string[] strTimeSpans = new[]{"00hr:00min:17sec","00hr:03min:18sec","00hr:05min:25sec","01hr:39min:44sec"}; TimeSpan[] timespans = strTimeSpans .Select(s =&gt; { var parts = s.Split(':'); int hour = int.Parse(parts[0].Substring(0, 2)); int min = int.Parse(parts[1].Substring(0, 2)); int sec = int.Parse(parts[2].Substring(0, 2)); return new TimeSpan(hour, min, sec); }).ToArray(); </code></pre> <p>Then you can use <a href="https://stackoverflow.com/a/1248/284240">this code</a> to get a readable <code>TimeSpan</code>, i have added it to a method:</p> <pre><code>public static string GetReadableTimespan(TimeSpan ts) { const int SECOND = 1; const int MINUTE = 60 * SECOND; const int HOUR = 60 * MINUTE; const int DAY = 24 * HOUR; const int MONTH = 30 * DAY; double delta = Math.Abs(ts.TotalSeconds); if (delta &lt; 0) { return "not yet"; } if (delta &lt; 1 * MINUTE) { return ts.Seconds == 1 ? "one second ago" : ts.Seconds + " seconds ago"; } if (delta &lt; 2 * MINUTE) { return "a minute ago"; } if (delta &lt; 45 * MINUTE) { return ts.Minutes + " minutes ago"; } if (delta &lt; 90 * MINUTE) { return "an hour ago"; } if (delta &lt; 24 * HOUR) { return ts.Hours + " hours ago"; } if (delta &lt; 48 * HOUR) { return "yesterday"; } if (delta &lt; 30 * DAY) { return ts.Days + " days ago"; } if (delta &lt; 12 * MONTH) { int months = Convert.ToInt32(Math.Floor((double)ts.Days / 30)); return months &lt;= 1 ? "one month ago" : months + " months ago"; } else { int years = Convert.ToInt32(Math.Floor((double)ts.Days / 365)); return years &lt;= 1 ? "one year ago" : years + " years ago"; } } </code></pre> <p>Now this....</p> <pre><code>foreach (TimeSpan ts in timespans) Console.WriteLine(GetReadableTimespan(ts)); </code></pre> <p>outputs:</p> <pre><code>17 seconds ago 3 minutes ago 5 minutes ago 1 hours ago </code></pre> <p><a href="http://ideone.com/YyCPiq" rel="nofollow noreferrer"><strong>Demo</strong></a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload