DateTime to simple recognized string (Facebook time format)

// DateAndTime Helper
public static string GetDateAndTimeFormNow(this HtmlHelper helper, string datetime)
{
    string value = string.Empty;

    if (!string.IsNullOrEmpty(datetime))
    {
        DateTime d1 = DateTime.Now;
        DateTime d2 = Convert.ToDateTime(datetime);

        TimeSpan t = d1 - d2;

        if (t.TotalDays >= 2)
            value = Math.Round(t.TotalDays, 0).ToString() + " days ago.";
        else if (t.TotalDays >= 1)
            value = "a day ago.";
        else if (t.TotalHours >= 2)
            value = Math.Round(t.TotalHours, 0).ToString() + " hours ago.";
        else if (t.TotalHours >= 1)
            value = "an hour ago.";
        else if (t.TotalMinutes >= 2)
            value = Math.Round(t.TotalMinutes, 0).ToString() + " minutes ago.";
        else
            value = "a minute ago.";
    }

    return value;
}
Advertisements