Login

BritishDateField

Author:
simon
Posted:
March 21, 2007
Language:
Python
Version:
Pre .96
Score:
2 (after 2 ratings)

The date field in the newforms module includes American style mm/dd/yyyy , which anyone outside the US will recognise as being complete madness. This date field behaves sensibly.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# The newforms library has these, but uses American style m/d/Y for two of 
# them.
BRITISH_DATE_INPUT_FORMATS = (
    '%Y-%m-%d', '%d/%m/%Y', '%d/%m/%y', # '2006-10-25', '25/10/2006', '25/10/06'
    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006'
    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006'
    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006'
    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006'
)

class BritishDateField(forms.DateField):
    def __init__(self, *args, **kwargs):
        super(BritishDateField, self).__init__(
            BRITISH_DATE_INPUT_FORMATS, *args, **kwargs
        )

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 12 months ago

Comments

Please login first before commenting.