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>
Related posts: