views:

99

answers:

3

Possible Duplicate:
How do I calculate relative time?

I am searching for a custom control for asp.net, which helps display user friendly dates, like instead of article date:

(2-april-2010)

it displays

(2 months old)

I am unable to find it on google, please can any one suggest links, custom controls, articles for the same.

Thank you.

+3  A: 

Try this (this is a datetime extension). So usage is:

var date = new DateTime.Now;

string formattedDate = date.ToReadableTimespan();


public static string ToReadableTimespan(this DateTime d)
            {
                // 1.
                // Get time span elapsed since the date.
                TimeSpan s = DateTime.Now.Subtract(d);

                // 2.
                // Get total number of days elapsed.
                int dayDiff = (int)s.TotalDays;

                // 3.
                // Get total number of seconds elapsed.
                int secDiff = (int)s.TotalSeconds;

                // 4.
                // Don't allow out of range values.
                if (dayDiff < 0 || dayDiff >= 31)
                {
                    return null;
                }

                // 5.
                // Handle same-day times.
                if (dayDiff == 0)
                {
                    // A.
                    // Less than one minute ago.
                    if (secDiff < 60)
                    {
                        return "just now";
                    }
                    // B.
                    // Less than 2 minutes ago.
                    if (secDiff < 120)
                    {
                        return "1 minute ago";
                    }
                    // C.
                    // Less than one hour ago.
                    if (secDiff < 3600)
                    {
                        return string.Format("{0} minutes ago",
                            Math.Floor((double)secDiff / 60));
                    }
                    // D.
                    // Less than 2 hours ago.
                    if (secDiff < 7200)
                    {
                        return "1 hour ago";
                    }
                    // E.
                    // Less than one day ago.
                    if (secDiff < 86400)
                    {
                        return string.Format("{0} hours ago",
                            Math.Floor((double)secDiff / 3600));
                    }
                }
                // 6.
                // Handle previous days.
                if (dayDiff == 1)
                {
                    return "yesterday";
                }
                if (dayDiff < 7)
                {
                    return string.Format("{0} days ago",
                        dayDiff);
                }
                if (dayDiff < 31)
                {
                    return string.Format("{0} weeks ago",
                        Math.Ceiling((double)dayDiff / 7));
                }
                return null;
            }
Richard
A: 

Nice code Richard.

I also like to propose this code http://www.codeproject.com/KB/datetime/DateDurationCalculation1.aspx that I use.

Aristos
A: 

The best approach (IMHO) if using MVC 2 would be to take Richard's code and hook it up through a DisplayTemplate (check out this blog to get started on display templates: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html).

...and then you just call the DisplayTemplate from your View by using the following syntax:

<% =Html.DisplayFor(model => model.ArticleDate, "ArticleDateDisplayTemplateName") %>

I have been using DisplayTemplates for a while now and they have been lifesavers for this type of thing, because you can use them throughout your application.

mc2thaH