Whats the best way to get a datetime from sql server and convert it to use friendly strings like so:
if its more than 1 day > 1 Day ago.
more than 7 days > 1 weeks ago
1 year ago
3 minutes ago
56 seconds ago.  
etc. Can this be done easily?
Whats the best way to get a datetime from sql server and convert it to use friendly strings like so:
if its more than 1 day > 1 Day ago.
more than 7 days > 1 weeks ago
1 year ago
3 minutes ago
56 seconds ago.  
etc. Can this be done easily?
 
    
     
    
    Your best bet is to make a DateTime extension method:
public static class DateTimeExtensions
{
    public static string ToRelative(this DateTime value)
    {        
        DateTime now = DateTime.Now; //maybe UtcNow if you're into that
        TimeSpan span = new TimeSpan(now.Ticks - value.Ticks);
        double seconds = Math.Abs(ts.TotalSeconds);
        if (seconds < 60)
            return string.Format("{0} seconds ago", span.Seconds);
        if (seconds < 2700)
            return string.Format("{0} minutes ago", span.Minutes);
        if (seconds < 86400)
            return string.Format("{0} hours ago", span.Hours);
        // repeat for greater "ago" times...
    }
}
Then make your call on your DateTime value like so:
myDateTime.ToRelative();
