15

Possible Duplicate:
How do I calculate relative time?

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


  • 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. - Nathan Taylor
  • 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. - Shog9
  • This really shouldn't have been closed... Please vote to reopen. - Nathan Taylor

2 답변


23

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


  • 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? - emzero
  • 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

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!


  • Small nitpick, you have pluralisation errors with this function - Tom Gullen

Linked


Related

Latest