Search code examples
jqgridjqgrid-formatter

jqGrid date formatting generates wrong AM/PM


I have column in a jqgrid modeled as follows:

colModel: [
          ...
          { name: 'TranDate', index: 'TranDate', search: false, width: 150, 
            sorttype: 'date', formatter: 'date', 
            formatoptions: { srcformat: 'Y-m-d H:i:s A', newformat: 'd-M-Y h.i A'} },
         ...
         ],

Data returned from JSON has the following format 2012-07-06 3:25:19 PM but when the displayed on the grid using the above formatting, it switches the PM to AM except for times between 12:01 to 12-59.

This question is similar to AM/PM date formatting in jqGrid but even after making modifications, the problem persists.

What am I missing or doing wrong?


Solution

  • The problem is that the DateFormat function in jquery.fmatter.js does not support AM/PM in srcformat:

        // Tony Tomov
        // PHP implementation. Sorry not all options are supported.
        // Feel free to add them if you want
        DateFormat : function (format, date, newformat, opts)  {
    

    Specifically, you can see below that there is no support for the A option when parsing the given date:

            } else {
                date = String(date).split(/[\\\/:_;.,\t\T\s-]/);
                format = format.split(/[\\\/:_;.,\t\T\s-]/);
                // parsing for month names
                for(k=0,hl=format.length;k<hl;k++){
                    if(format[k] == 'M') {
                        dM = $.inArray(date[k],dateFormat.i18n.monthNames);
                        if(dM !== -1 && dM < 12){date[k] = dM+1;}
                    }
                    if(format[k] == 'F') {
                        dM = $.inArray(date[k],dateFormat.i18n.monthNames);
                        if(dM !== -1 && dM > 11){date[k] = dM+1-12;}
                    }
                    if(date[k]) {
                        ts[format[k].toLowerCase()] = parseInt(date[k],10);
                    }
                }
                if(ts.f) {ts.m = ts.f;}
                if( ts.m === 0 && ts.y === 0 && ts.d === 0) {
                    return "&#160;" ;
                }
                ts.m = parseInt(ts.m,10)-1;
                var ty = ts.y;
                if (ty >= 70 && ty <= 99) {ts.y = 1900+ts.y;}
                else if (ty >=0 && ty <=69) {ts.y= 2000+ts.y;}
                timestamp = new Date(ts.y, ts.m, ts.d, ts.h, ts.i, ts.s, ts.u);
            }
    

    You have a few options. If you can modify the web service you can have it return the dates in a different format (such as a unix timestamp) or have it return a new date column in a supported format. Alternatively you can report this as a jqGrid bug and/or fix this section of the code to support AM/PM specifiers.