Click here to Skip to main content
15,885,203 members
Articles / Programming Languages / C#
Tip/Trick

Extensions: User friendly version of TimeSpan #1

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Oct 2012GPL31 min read 14.2K   2   4
In this post I'll show a simple extension method which is very helpful if you work on websites / apps where you've to display relative time to users.

In my last post of this series, I told about basics of using and creation of extension methods in .NET (C#). I also told about a extension which delays a method for given time. In you have not seen it yet, please see it here.

So, you have now the power of creating one of the really useful things in .NET. In this post I'll show a simple extension method which is very helpful if you work on websites / apps where you've to display relative time to users. OK, by relative time I mean, time format like x minutes, y seconds ago. This is very user friendly as displaying a time format like Oct 28, 2012 16:45 is not very easy to get if currently time is Oct 28, 2012 16:50. It would be easier if it was like this 5 minutes ago. So, I hope you get what I wanted to tell.

To solve this problem, I made an extension method after reviewing lot of sites. This method converts a TimeSpan into a user friendly string either shorthand or not. In shorthand mode, min = minute(s), s = second(s), h = hour(s), d = day(s), yr = year(s).

TimeSpan.ToFriendlyString1
C#
///<summary> 
///Formats the time to user safe string. 
///</summary> 
///<param name="ts">The ts.</param> 
///<param name="shorthand">if set to <c>true</c>[shorthand].</param> 
///<returns>...</returns> 
public static string ToFriendlyString1(this TimeSpan ts, bool shorthand) 
{ 
    if(ts == TimeSpan.MaxValue || ts == TimeSpan.MinValue) return"Sometime";
    StringBuilder sb = newStringBuilder();
    
    if(!shorthand) 
    { 
        if((int)ts.Days == 365.0 || (int)ts.Days == 366.0) sb.Append("1 year, ");
        else if(ts.Days > 365.0) sb.Append((int)(ts.Days / 365.0) + " years, ");
        else if(ts.Days == 1.0) sb.Append("1 day, "); 
        else if(ts.Days > 1.0) sb.Append((int)ts.Days + " days, ");
        if(ts.Hours == 1.0) sb.Append("1 hour, "); 
        else if(ts.Hours > 1.0) sb.Append((int)ts.Hours + " hours, ");
        if(ts.Minutes == 1.0) sb.Append("1 minute, "); 
        else if(ts.Minutes > 1.0) sb.Append((int)ts.Minutes + " minutes, ");
        if(ts.Seconds == 1.0) sb.Append("1 second, "); 
        else if(ts.Seconds > 1.0) sb.Append((int)ts.Seconds + " seconds, "); 
    } 
    else 
    { 
        if((int)ts.Days == 365.0 || (int)ts.Days == 366.0) sb.Append("1yr, "); 
        else if(ts.Days > 365.0) sb.Append((int)(ts.Days / 365.0) + "yr, "); 
        else if(ts.Days == 1.0) sb.Append("1d, "); 
        else if(ts.Days > 1.0) sb.Append((int)ts.Days + "d, ");
        if(ts.Hours == 1.0) sb.Append("1h, "); 
        else if(ts.Hours > 1.0) sb.Append((int)ts.Hours + "h, ");
        if(ts.Minutes == 1.0) sb.Append("1min, "); 
        else if(ts.Minutes > 1.0) sb.Append((int)ts.Minutes + "min, ");
        if(ts.Seconds == 1.0) sb.Append("1s, "); 
        else if(ts.Seconds > 1.0) sb.Append((int)ts.Seconds + "s, "); 
    }
    sb.Remove(sb.Length -2, 2);
    return sb.ToString(); 
}

Usage

C#
(DateTime.Now - DateTime.Now.AddHours(-3.5)).ToFriendlyString1(false) + " ago"
// 3 hours, 29 minutes, 59 seconds ago

 "After " + TimeSpan.FromDays(365 * 2.56897).ToFriendlyString1(false)
// After 2 years, 16 hours, 10 minutes, 37 seconds

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Student
India India

Comments and Discussions

 
GeneralVote of 5 Pin
the headless nick31-Oct-12 19:51
professionalthe headless nick31-Oct-12 19:51 
SuggestionAnother version Pin
Ky7m30-Oct-12 4:54
Ky7m30-Oct-12 4:54 
QuestionCategory Pin
Guirec30-Oct-12 4:05
professionalGuirec30-Oct-12 4:05 
AnswerRe: Category Pin
Code098730-Oct-12 5:34
Code098730-Oct-12 5:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.