19

Possible Duplicate:
How do I calculate relative time?

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

3
  • 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 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. Commented Mar 14, 2011 at 19:08
  • 1
    Including details on how 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. Commented Mar 14, 2011 at 19:23
  • This really shouldn't have been closed... Please vote to reopen. Commented Mar 15, 2011 at 17:28

2 Answers 2

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();
});
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
19

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!

1 Comment

Small nitpick, you have pluralisation errors with this function

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.