jqgrid date formatter example?

Mark Jones picture Mark Jones · Oct 29, 2011 · Viewed 22.4k times · Source

Does anybody have an example of using the date formatter with a server side database, or can you point me to something to help?

Answer

flanth picture flanth · Oct 29, 2011

You can find information about predefined formatters on the jqGrid wiki.

The following is an example of how date formatting can be used in the grid. The format ShortDate displays the date according to the selected locale. You can use your own formatting instead, for example Y-m-d H:i:s.

srcformat describes the format of the date as sent by the server, newformat describes the desired output format.

This example includes searchoptions which will make sure that your users can select the desired date with the help of a datepicker when performing a search on the grid.

colModel :[ 
    { name:'startdate', index:'startdate', formatter:'date', 
        formatoptions: { srcformat:'m/d/Y', newformat:'ShortDate' }, 
        searchoptions: { sopt: ['eq','lt','le','gt','ge'],
            dataInit : function (elem) { 
                $(elem).datepicker({ changeMonth: true, changeYear: true, 
                    dateFormat: 'yy-mm-dd' }); 
            }
        }
    }
]