DaysSince() extension method like in Rails

by ryudice on June 11, 2008

This is an extension method that will return a string containing how many days have passed since the date until today’s date. It is basically the same as the “distance_of_time_in_words_to_now” from the rails framework.

public static class ExtensionMethods
{
public static string DaysSince(this DateTime dt)
{
int days = DateTime.Now.Subtract(dt).Days;
return days + ((days > 1) ? ” days ” : ” day”) + ” ago”;
}
}

And here is how you can use the function in a datagrid:

<asp:TemplateField>
<ItemTemplate>
<asp:Label ID=”Label7″ runat=”server” Text=<%# ((DateTime)Eval(“OrderDate”)).DaysSince() %>></asp:Label>
</ItemTemplate>
</asp:TemplateField>

The result:
DataGrid

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Twitter

Related posts:

  1. Label de varias lineas en ASP.NET usando C#
  2. Deshabilitar una fecha o dia en el control de calendario en ASP.NET
  3. Funcion fibonacci con recursion
  4. Funcion para palindromo en .NET y C#

Leave a Comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes

PensandoEnCodigo is Digg proof thanks to caching by WP Super Cache