Login

Human format Date representation

Author:
sachingupta006
Posted:
March 20, 2012
Language:
Python
Version:
1.3
Tags:
date humanize
Score:
0 (after 0 ratings)

This template tag, takes the date format of the datetime object and returns the date in a human readable format.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@register.filter(name='date_human')
def humanize_date(time):
    if not time:
        return

    format = "%b %d"
    current = datetime.datetime.now().day
    object_date =  time.day

    if current - object_date == 0:
        return "today"

    if current - object_date == 1:
        return "yesterday"

    else:
        if not current.year == time.year:
            format = 'n/j/y'
        date_str = time.strftime(format)
        return "%s" % date_str

More like this

  1. date range by mackenzie_kearl 7 years, 2 months ago
  2. Format date range by bmihelac 6 years ago
  3. datemate by catellar 3 years, 7 months ago
  4. Gmail date format by vizualbod 3 years, 11 months ago
  5. Smart i18n date diff (twitter like) by Batiste 6 years ago

Comments

Please login first before commenting.