parse date template tag

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import datetime

from django.template import Library
from django.template.defaultfilters import stringfilter

register = Library()

@stringfilter
def parse_date(date_string, format):
    """
    Return a datetime corresponding to date_string, parsed according to format.

    For example, to re-display a date string in another format::

        {{ "01/01/1970"|parse_date:"%m/%d/%Y"|date:"F jS, Y" }}

    """
    try:
        return datetime.datetime.strptime(date_string, format)
    except ValueError:
        return None

register.filter(parse_date)

More like this

  1. Parse datetime model field to string by jzelez 1 year, 7 months ago
  2. Template-Filter for Feedparser-Dates by kioopi 2 years, 7 months ago
  3. Smart i18n date diff (twitter like) by Batiste 2 years, 10 months ago
  4. Natural language date/time form fields by jdriscoll 4 years, 8 months ago
  5. datetime.time/datetime.datetime to Unix Epoch (with microsecond support) by sleepycal 1 year, 9 months ago

Comments

(Forgotten your password?)