Click here to Skip to main content
Click here to Skip to main content
Go to top

Extensions: User friendly version of TimeSpan #1

, 30 Oct 2012
Rate this:
Please Sign up or sign in to vote.
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
///<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

(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)

Share

About the Author


Comments and Discussions

 
GeneralVote of 5 Pinmemberthe headlessnick31-Oct-12 19:51 
SuggestionAnother version PinmemberKy7m30-Oct-12 4:54 
QuestionCategory PinmemberGuirec Le Bars30-Oct-12 4:05 
AnswerRe: Category PinmemberCode098730-Oct-12 5:34 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 30 Oct 2012
Article Copyright 2012 by Code0987
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid