Date format in CRUD forms

502 views
Skip to first unread message

Jim Steil

unread,
Aug 9, 2011, 4:07:19 PM8/9/11
to web2py-users
I'm sure I just missed this somewhere, but I can't find how to control the date format for date fields in CRUD-created forms.

db.py....

driverUnit = db.define_table('driverUnit',
    Field('driverUnitId', 'id'),
    Field('driverId', db.driver, required=True, label='Driver'),
    Field('unitId', db.unit, required=True, label='Unit'),
    Field('start', 'date', required=True, label='Started On'))

How it displays in my form...


I'd like it to be mm/dd/yyyy format but don't know how to tell it to display that way.  What I'm finding on the web is just confusing me more.

    -Jim




Massimo Di Pierro

unread,
Aug 9, 2011, 5:59:05 PM8/9/11
to web2py-users
Field(...,requires=IS_DATE(format='%m/%d/%Y'))

On Aug 9, 3:07 pm, Jim Steil <j...@qlf.com> wrote:
> I'm sure I just missed this somewhere, but I can't find how to control
> the date format for date fields in CRUD-created forms.
>
> db.py....
>
> driverUnit = db.define_table('driverUnit',
>      Field('driverUnitId', 'id'),
>      Field('driverId', db.driver, required=True, label='Driver'),
>      Field('unitId', db.unit, required=True, label='Unit'),
>      Field('start', 'date', required=True, label='Started On'))
>
> How it displays in my form...
>

Jim Steil

unread,
Aug 10, 2011, 8:58:04 AM8/10/11
to web...@googlegroups.com
Massimo

Thank you for the reply. However, it still isn't working correctly for
me. I think I may have explained the problem wrong. When I go in to
edit a record, the date displays with the %m/%d/%Y format as I want.
But, when I select a date from the calendar widget it places the date
into the field in %Y-%d-%m format. What I really want to do is force
the calendar date widget to represent its date in %m/%d/%Y format.
Sorry for the misleading....

-Jim

Anthony

unread,
Aug 10, 2011, 10:39:36 AM8/10/11
to web...@googlegroups.com
The calendar widget date format is set in /views/web2py_ajax.html. Note, it is actually a translated string, so you can change it by adding it to your translation files (or just directly edit web2py_ajax.html if you just need a single fixed format). You could also re-assign the value of the w2p_ajax_date_format variable (in Javascript) in your layout.html file at some point _after_ web2py_ajax.html has been included.
 
Anthony

On Wednesday, August 10, 2011 8:58:04 AM UTC-4, Jim S wrote:
Massimo

Thank you for the reply.  However, it still isn't working correctly for
me.  I think I may have explained the problem wrong.  When I go in to
edit a record, the date displays with the %m/%d/%Y format as I want.  
But, when I select a date from the calendar widget it places the date
into the field in %Y-%d-%m format.  What I really want to do is force
the calendar date widget to represent its date in %m/%d/%Y format.  
Sorry for the misleading....

     -Jim


On 8/9/2011 4:59 PM, Massimo Di Pierro wrote:
> Field(...,requires=IS_DATE(format='%m/%d/%Y'))
>

> On Aug 9, 3:07 pm, Jim Steil<j....@qlf.com>  wrote:

Jim Steil

unread,
Aug 10, 2011, 10:40:51 AM8/10/11
to web...@googlegroups.com
Thanks Anthony, that did the trick.  I apologize for not stating my problem properly in the first post. 

    -Jim

Simon Carr

unread,
Aug 18, 2012, 11:52:22 AM8/18/12
to web...@googlegroups.com
I have created a short video on how to change the default Ajax date format in Web2Py. This in turn allows the Calendar widget to return what ever date format you want.


Cheers
Simon

Massimo Di Pierro

unread,
Sep 1, 2012, 1:25:38 PM9/1/12
to web...@googlegroups.com
Actually simon that is not how you should do it because it will break stuff.

The data format appears in two places:
- web2py.js for the popup calendar
- the IS_DATE(), IS_DATETIME() validators.

They have to be the same in  both. They are always wrapped in a T(...) so all you should need to do it add the proper translation for the for strings using the languages interface. You should not change it for all languages.

if you really want to change for all languages, as you did. make sure you also change it in the validators otherwise it will break for visitors with different language settings.
Reply all
Reply to author
Forward
0 new messages