m-u
10/24/2013 - 2:55 PM

time timespan in words - return relative time between two given dates

time timespan in words - return relative time between two given dates

public string TimespanInWords(DateTime oDtTo, DateTime oDtFrom)
{
    TimeSpan oTs = oDtTo >= oDtFrom ? oDtTo - oDtFrom : oDtFrom - oDtTo;
    if (oTs.TotalSeconds < 30)
        return "less than a minute";
    if (oTs.TotalSeconds < 90)
        return "1 minute";
    if (oTs.TotalSeconds < 44 * 60 + 30)
        return (int)oTs.TotalMinutes + " minutes";
    if (oTs < new TimeSpan(0, 89, 30))
        return "about 1 hour";
    if (oTs < new TimeSpan(23, 59, 30))
        return "about " + oTs.Hours + " hours";
    if (oTs < new TimeSpan(41, 59, 30))
        return "1 day";
    if (oTs < new TimeSpan(29, 23, 59, 30))
        return Convert.ToInt32(oTs.TotalDays) + " days";
    if (oTs < new TimeSpan(59, 23, 59, 30))
        return "about 1 month";
    if (oTs < new TimeSpan(365, 0, 0, 0))
        return (Convert.ToInt32(oTs.TotalDays) / 30) + " months";
    if (oTs < new TimeSpan(456, 0, 0, 0))
        return "about 1 year";
    if (oTs < new TimeSpan(639, 0, 0, 0))
        return "over 1 year";
    if (oTs < new TimeSpan(730, 0, 0, 0))
        return "about 2 years";
    return "about " + (oTs.Days / 365) + " years";
}