18

Possible Duplicate:
How do I calculate relative time?

Is there anything similar to rails' time_ago_in_words helper for asp.net MVC?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
ryudice
  • 36,476
  • 32
  • 115
  • 163
  • It's difficult to be certain because I have not personally used rails or the helper he is referring to, but based on the [context](http://bit.ly/h7zYCS) in the documentation, it looks to be similar to an HtmlHelper in ASP.NET MVC. That being said, I don't believe this is a duplicate. Yes, the code in the suggested duplicate would work, but that is a purely server-side approach. The desired output does not need to be generated server-side and therefore the answer I have provided which differs from the duplicate is still pertinent. **Don't close this question**. – Nathan Taylor Mar 14 '11 at 19:08
  • 1
    Including details on how [`time_ago_in_words`](http://apidock.com/rails/ActionView/Helpers/DateHelper/time_ago_in_words) operates would probably go a long way toward helping folks answer this effectively. Including details on how *you* want to use it would be *even better*. – Shog9 Mar 14 '11 at 19:23
  • This really shouldn't have been closed... *Please vote to reopen.* – Nathan Taylor Mar 15 '11 at 17:28

2 Answers2

25

Depending on your intended output target, the jQuery plugin Timeago may be a better option.

Here's an HtmlHelper to create an <abbr /> element containing an ISO 8601 timestamp:

public static MvcHtmlString Timeago(this HtmlHelper helper, DateTime dateTime) {
    var tag = new TagBuilder("abbr");
    tag.AddCssClass("timeago");
    tag.Attributes.Add("title", dateTime.ToString("s") + "Z");
    tag.SetInnerText(dateTime.ToString());

    return MvcHtmlString.Create(tag.ToString());
}

Combine the above helper's output with the following JavaScript somewhere on your page and you'll be in the money.

<script src="jquery.min.js" type="text/javascript"></script>
<script src="jquery.timeago.js" type="text/javascript"></script>

jQuery(document).ready(function() {
  jQuery("abbr.timeago").timeago();
});
mbillard
  • 38,386
  • 18
  • 74
  • 98
Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • If I pass a dateTime.ToUniversal() date the timeago calculates the relative time properly but the tooltip shows the time in UTC timezone instead of the client computer timezone. What am I'm missing? – empz Mar 14 '14 at 02:11
  • I really love this, thanks! I think that the original output of the date could be more friendly though in `SetInnerText`, I will be changing that so that it degrades nicely. – Luke Oct 06 '14 at 09:32
18

I'm currently using the following extension method. Not sure if it's the best one available out there.

public static string ToRelativeDate(this DateTime dateTime)
{
    var timeSpan = DateTime.Now - dateTime;

    if (timeSpan <= TimeSpan.FromSeconds(60))
        return string.Format("{0} seconds ago", timeSpan.Seconds);

    if (timeSpan <= TimeSpan.FromMinutes(60))
        return timeSpan.Minutes > 1 ? String.Format("about {0} minutes ago", timeSpan.Minutes) : "about a minute ago";

    if (timeSpan <= TimeSpan.FromHours(24))
        return timeSpan.Hours > 1 ? String.Format("about {0} hours ago", timeSpan.Hours) : "about an hour ago";

    if (timeSpan <= TimeSpan.FromDays(30))
        return timeSpan.Days > 1 ? String.Format("about {0} days ago", timeSpan.Days) : "yesterday";

    if (timeSpan <= TimeSpan.FromDays(365))
        return timeSpan.Days > 30 ? String.Format("about {0} months ago", timeSpan.Days / 30) : "about a month ago";

    return timeSpan.Days > 365 ? String.Format("about {0} years ago", timeSpan.Days / 365) : "about a year ago";
}

The helper should be somthing like this:

public MvcHtmlString Timeago(this HtmlHelper helper, DateTime dateTime) 
{
    return MvcHtmlString.Create(dateTime.ToRelativeDate());
}

Hope it helps!

goenning
  • 6,514
  • 1
  • 35
  • 42