Source:
from datetime import datetime

def pretty_date(time=False):
    now = datetime.now()
    if type(time) is int:
        diff = now - datetime.fromtimestamp(time)
    elif isinstance(time, datetime):
        diff = now - time
    elif not time:
        diff = now - now
    second_diff = diff.seconds
    day_diff = diff.days
    if day_diff < 0:
        tense = 'from now'
        second_diff = 86400 - second_diff
        day_diff += 1
        day_diff *= -1
    else:
        tense = 'ago'

    if day_diff == 0:
        if second_diff < 10:
            return 'just now' if tense == 'ago' else 'in few seconds'
        elif second_diff < 60:
            return str(second_diff) + ' seconds ' + tense
        elif second_diff < 120:
            return  'a minute ' + tense
        elif second_diff < 3600:
            return str( second_diff / 60 ) + ' minutes ' + tense
        elif second_diff < 7200:
            return 'an hour ' + tense
        elif second_diff < 86400:
            return str( second_diff / 3600 ) + " hours " + tense
    if day_diff == 1:
        return 'Yesterday' if tense == 'ago' else 'Tomorrow'
    elif day_diff < 7:
        return str(day_diff) + ' days ' + tense
    elif day_diff == 7:
        return 'a week ' + tense
    elif day_diff < 14:
        return '2 weeks ' + tense
    elif day_diff < 31:
        return str(day_diff/7) + ' weeks ' + tense
    elif day_diff == 31:
        return 'a month ' + tense
    elif day_diff < 60:
        return '2 months ' + tense
    elif day_diff < 365:
        return str(day_diff/30) + ' months ' + tense
    elif day_diff == 365:
        return 'a year ' + tense
    elif day_diff < 365 * 2:
        return '2 years ' + tense
    return str(day_diff/365) + ' years ' + tense
Sample Usage:
# passing a datetime
some_past_date = datetime.strptime('2012-01-01 12:00:00', "%Y-%m-%d %H:%M:%S")
print pretty_date(some_past_date)

Converts a timestamp or datetime to a nice looking time that make sense like 5 days ago, 3 minutes ago etc.

Pythoneer 6 years ago