Search code examples
jqueryjqgridfree-jqgrid

Free jqGrid 4.9.0 - Dates format on Safari on MAC


Testing date format options on all major 4 browsers (Safari, Chrome, IE & Firefox) while working on windows works well.

However, when testing it on MAC machines, the format option failed within Safari (Firefox on MAC works well).

My format option is:

gridField.formatter = 'date';
gridField.formatoptions = {};
gridField.formatoptions['srcformat'] = 'U/1000';
gridField.formatoptions['newformat'] = 'm/d/Y H:i:s';

Which in Safari (on a MAC only) will yield: NaN/NaN/NaN NaN:Nan:NaN.

Any idea how to overcome the problem?

Thanks,


Solution

  • The problem is the following. Free jqGrid uses mostly the same code of $.jgrid.parseDate function like jqGrid 4.7. It support the usage "u" and "U" in the date format in two different cases. The first case is the usage of "u" and "U" without any additional format specification (for example, srcformat:"u"). It means that jqGrid uses new Date(inputValue*1000) to parse the input value. The input value like 1418297439 will be displayed as 12/11/2014 11:30:39 using newformat: "m/d/Y H:i:s". On the other side you have 1418297439000 instead of 1418297439 in the input data and jqGrid don't have some exact formatter for the case.

    You use format srcformat: "U/1000" in your original demo http://jsfiddle.net/OlegKi/ngm5rhgp/7/. Such format ("U/1000") don't exist at all. The format "U/1000" will be interpretet in the same way like "U/", "U.", "U/BlaBla", "U:H:i:s" or any other which start with u following with separator. Because the input data looks like 1418297439000 and have no additional separators (,, /, , , and some other) then only the first formatter U will be used, but it will be interpreted now as u formatter, which means millisecond. The u format will be used typically for the format like 12/11/2014 11:30:39,123 where tha last 123 part is the millisecond part of the time.

    It seems that Safari on MAC don't allow to create the date as new Date(1970, 1, 1, 0, 0, 1418297439000) which uses jqGrid with 1418297439000 as input data and srcformat: "U/1000".

    What I suggest you to do is modifying the input data and the usage of srcformat: "u" format. One need to enumerate all items of input data and to devide start_time and end_time properties to 100. I used in my demo http://jsfiddle.net/OlegKi/ngm5rhgp/8/ the code

    var mydata = [{...},{...}... {...}], n = mydata.length, item, i;
    
    for (i = 0; i < n; i++) {
        item = mydata[i];
        item.start_time = Math.floor(item.start_time / 1000);
        item.end_time = Math.floor(item.end_time / 1000);
    }
    

    and have replaces srcformat: "U/1000" to srcformat: "u". If you have not "local" value of datatype than you can modify the data inside of beforeProcessing callback.

    UPDATED: To simplify processing of the time in milliseconds since the Unix Epoch (January 1 1970 00:00:00 GMT), like 1418297439000, I introduced in free jqGrid new format option: srcformat: "u1000". The new demo http://jsfiddle.net/OlegKi/ngm5rhgp/9/ uses unmodified input data and just use srcformat: "u1000" instead of srcformat: "U/1000". To use it one have to use the latest free jqGrid from GitHub.