Naturaltime with minutes, hours, days and weeks
<p>I use naturaltime in my Django application.</p>

<p>How can I display only time in minutes, then hours, then days, then weeks?</p>

<p>Here is my code:</p>

<pre><code>{{ obj.pub_date|naturaltime }}
</code></pre>

<p>I use two custom filters to solve a very similar problem using babel and pytz. I write "Today" or "Yesterday" plus the time. You're welcome to use my code any way you like. </p>

<p>My template code is two usages, one for writing "Today", "Yesterday", or the date if it was even earlier. </p>

<p><code>{{ scored_document.fields.10.value|format_date_human(locale='en') }}</code></p>

<p>Then this tag writes the actual time of day </p>

<p><code>{{ scored_document.fields.10.value|datetimeformat_list(hour=scored_document.fields.17.value|int ,minute =scored_document.fields.18.value|int, timezoneinfo=timezoneinfo, locale=locale) }}</code></p>

<p>The two corresponding functions are</p>

<pre><code>MONTHS = ('Jan.', 'Feb.', 'Mar.', 'April.', 'May.', 'June.',
          'July.', 'Aug.', 'Sep.', 'Oct.', 'Nov.', 'Dec.')
FORMAT = '%H:%M / %d-%m-%Y'

def format_date_human(to_format, locale='en', timezoneinfo='Asia/Calcutta'):
    tzinfo = timezone(timezoneinfo)
    now = datetime.now()
    #logging.info('delta: %s', str((now - to_format).days))
    #logging.info('delta2: %s', str((datetime.date(now)-datetime.date(to_format)).days))

    if datetime.date(to_format) == datetime.date(now):
        date_str = _('Today')
    elif (now - to_format).days == 1:
        date_str = _('Yesterday')
    else:
        month = MONTHS[to_format.month - 1]
        date_str = '{0} {1}'.format(to_format.day, _(month))
    time_str = format_time(to_format, 'H:mm', tzinfo=tzinfo, locale=locale)
    return "{0}".format(date_str, time_str)



def datetimeformat_list(date, hour, minute, locale='en', timezoneinfo='Asia/Calcutta'):

    import datetime as DT
    import pytz

    utc = pytz.utc
    to_format = DT.datetime(int(date.year), int(date.month), int(date.day), int(hour), int(minute))
    utc_date = utc.localize(to_format)
    tzone = pytz.timezone(timezoneinfo)
    tzone_date = utc_date.astimezone(tzone)
    time_str = format_time(tzone_date, 'H:mm')
    return "{0}".format(time_str)
</code></pre>

python django