Local date format in INPUT tag

29 views
Skip to first unread message

Mirek Zvolský

unread,
Oct 19, 2010, 10:31:48 AM10/19/10
to web2py-users
Hi,
I'm new in web2py (and web programming at all).

Can anybody help me, how should I display dates in <input> (I want use
SQLFORM())?

At this time I see 2010-10-19 and same I receive from javascript
calendar.

However proper local setting for our country is 19.10.2010 and of
course users want to see this that way in the web form.

What I should do in model?

And more...
If I translate the application to the language wit same date format
dd.mm.YYYY, there is no additional problem. But if the country uses
different date format, I think formatting cannot be made in model.
Is there a property of T() object, where formatting can be set (and
changed based on current language).

I'm sorry for the basic question, however I cannot find the answer
here in user group messages nor in web2py book.
Thanks, Mirek

annet

unread,
Oct 19, 2010, 1:05:11 PM10/19/10
to web2py-users
Hi Mirek,

Here's an excerpt of my log that might be helpful:

Formatting date

Model file:

T.force('en-de') ## en-de is a language file you'll create below

In the table definition:

SQLField('fieldname', type='date')

... a validator on this field which reads like:

db.tablename.fieldname.requires=IS_NULL_OR(IS_DATE(str(T('%Y-%m-
%d'))))

Languages file:

... create a language file e.g. en-de.py and edit the dates:

Original Translation
%Y-%m-%d %d-%m-%Y
%Y-%m-%d %H:%M:%S %d-%m-%Y %H:%M:%S

Click update.

Views file:

... to use the date in a view:

{{=db.tablename.fieldname.formatter(tablename.fieldname)}}

If you use form_factory to create a custom form, the code in the
controller should read:

SQLField('fieldname',type='date',requires=IS_DATE(str(T('%Y-%m-
%d'))))

Kind regards,

Annet.
Reply all
Reply to author
Forward
0 new messages