tutorial que net mvc asp c# asp.net-mvc

c# - que - asp.net hace tiempo mvc en palabras helper



asp.net tutorial (2)

Actualmente estoy usando el siguiente método de extensión. No estoy seguro si es el mejor disponible por ahí.

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"; }

El ayudante debería ser algo así:

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

¡Espero eso ayude!

Posible duplicado:
¿Cómo calculo el tiempo relativo?

¿Hay algo similar al ayudante time_ago_in_words de rails para asp.net MVC?


Dependiendo de su objetivo de salida deseado, el complemento jQuery Timeago puede ser una mejor opción.

Aquí hay un HtmlHelper para crear un elemento <abbr /> que contiene una marca de tiempo ISO 8601 :

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 la salida del asistente anterior con el siguiente JavaScript en algún lugar de su página y estará en el dinero.

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