I'm looking for a function to format a date in order to get day, month and year. Dates are being stored in my database in the following format 2012-09-26.
Asked
Active
Viewed 4,264 times
4
-
So you have YYYY-MM-DD, and what should be the format of your output? – selllikesybok Sep 26 '12 at 16:13
-
Do you want to display it in a template or what is the use case? – XORcist Sep 26 '12 at 16:16
-
Yes, I need to display separate the day, month and year in a template. Thanks for reply! – Agustin.Ferreira Sep 26 '12 at 16:23
-
Is the date stored as an SQL date type or as a string? – Hans Then Sep 26 '12 at 16:40
1 Answers
11
If your goal is to display on web2py template, so you have to use pure Python to format
{{=row.datetime_field.strftime("%d/%m/%Y")}}
The above will generate 25/09/2012
Tale a look at Python strftime documentations.
If you want to show only the day.
{{=row.datetime_field.date}}
Also you can set it as a representation for that field
db.mytable.datetime_field.represent = lambda value, row: value.strftime("format-here")
The representation will be useful only on SQLFORM.grid and SQLTABLE
All you need is strftime

Bruno Rocha - rochacbruno
- 7,890
- 6
- 26
- 31