1

My Jqgrid's data source is a php which returns a json The date constraint in the returned json is in milliseconds I want to convert it to regular date format I tried searching for a solution the one that got real close to solution is the one given [below JQ Grid Date Format

formatter:'date', formatoptions: {srcformat: 'U', newformat:'d/m/Y'} But still it doesn't fix my issue as the date is not formulated right 24/10/146 which is not the correct date at all Looking forward to some worthy fixes or suggestions

1394085600000 which is the returned json data should essentially be 03/06/2014

thanks in advance

  • could you append our question with exact *example* of input data which you use for the grid. A demo which reproduce the problem would be the best. – Oleg Mar 12 '14 at 09:23

2 Answers2

4

There are many ways how to solve the problem. The source format srcformat: 'U' means (like other formats used by jqGrd) PHP format. Starting with PHP 5.2.2 it supports "u" format, but it means microseconds (μs) instead of milliseconds (ms) which you use (see here for example). So the input data should be integer or float value with is 1/1000 milliseconds to be successful processed by srcformat: 'U'. What jqGrid do to parse srcformat: "U" format will be clear from the following (see here) lines of the code

if( !isNaN( date - 0 ) && String(format).toLowerCase() === "u") {
    timestamp = new Date( parseFloat(date)*1000 );
}

So you need just adjust the values which you use as the input for srcformat: "U".

UPDATED: If your input data are like 1394085600000 and if you can't change it then you can use custom formatter which calls "date" formatter:

formatter: function (cellval, opts, rowObject, action) {
    return $.fn.fmatter.call(
              this,
              "date",
              new Date(cellval),
              $.extend({}, $.jgrid.formatter.date, opts),
              rowObject,
              action);
}

See the demo which use it.

UPDATED 2: Free jqGrid fork support new format option: srcformat: "u1000", which converts the date in long integer format like 1394085600000 in "03/06/2014". Thus to solve the problem one should just upgrade to the current version of free jqGrid. See the demo or the answer

I have to warn about the usage of pseudo-format srcformat: 'U/1000'. jqGrid (neither old or the new one) support the format. It can be used as a hack to display only the date, but other functionality will not work correctly (editing, filtering, sorting and so on) or don't work even for reading in some web browsers (see the question). Thus I strictly recommend don't use the hack whith srcformat: 'U/1000'.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks Oleg you did through some light into facts but unfortunately still doesn't work – Gotham's Reckoning Mar 12 '14 at 14:22
  • @MathewMGeorge: What doesn't work? I asked you to post the testdata which you used. If you post additionally the grid definition (1-2 column will be enough) then one could easy reproduce the problem and fix it. – Oleg Mar 12 '14 at 14:25
  • Sorry Oleg I was in the process of adding the sample data but the comment and got posted by mistake The sample data is 1394085600000 and essentially it has nothing to do with php as there is no data processing happening in PHP instead it just echos the json which it receives thanks – Gotham's Reckoning Mar 12 '14 at 15:27
  • @tpeczek can you some up with a solution for this as i have found you answering a similar kinda question http://stackoverflow.com/questions/8212008/in-jqgrid-how-to-format-timestamp-to-dd-mm-yyyy-format – Gotham's Reckoning Mar 12 '14 at 17:12
  • @MathewMGeorge: See **UPDATED** part of my answer with [the demo](http://www.ok-soft-gmbh.com/jqGrid/CustomDateFormater2.htm). – Oleg Mar 12 '14 at 19:54
  • can you please look into the issue given below and please help me if possible http://stackoverflow.com/questions/23659040/how-to-display-nested-json-object-in-jqgrid @oleg – Gotham's Reckoning May 14 '14 at 17:49
  • I tried this code but i am getting "Uncaught TypeError: Cannot read property 'date' of undefined" error because of '$.jgrid.formatter.date'. I did some research and i am including locale js file before jqgrid src js file. But still getting the same error – ivish Feb 16 '16 at 15:38
  • @ivish: Which version of jqGrid and from which fork ([free jqGrid](https://github.com/free-jqgrid/jqGrid), [Guriddo jqGrid JS](http://guriddo.net/?page_id=103334) or some old version of jqGrid in version <=4.7) you use jqGrid? Free jqGrid is the fork which I develop since the end of 2014. The `formatter: "date"` supports `formatoptions` with `srcformat: "u1000"`, which makes unneeded and additional codding. See [the answer](http://stackoverflow.com/a/33652984/315935) and [the demo](http://ok-soft-gmbh.com/jqGrid/OK/filterByAdditionalProperties.htm) for example. – Oleg Feb 16 '16 at 16:09
  • @ivish: See **UPDATED 2** part of my answer for more information. – Oleg Feb 16 '16 at 16:19
  • Thanks for reply Oleg. I am using Guriddo jqGrid JS V - 5.0.1. If i want to switch over to 'free jqGrid' will it work by just replacing existing js and css files? By the way currently i am just using basic jqGrid features. – ivish Feb 16 '16 at 18:03
  • @ivish: Yes, it works in the most cases. You can use URLs from CDN described in [the wiki article](https://github.com/free-jqgrid/jqGrid/wiki/Access-free-jqGrid-from-different-CDNs). Some options are simplified in free jqGrid and one can typically reduce the final code after the migration. You can find some simple examples [here](http://free-jqgrid.github.io/getting-started/index.html). Important is that the content from `grid.locale-en.min.js` is included in `jquery.jqgrid.min.js` (`jquery.jqgrid.src.js`). I will extend the information of the site free-jqgrid.github.io in the future. – Oleg Feb 16 '16 at 19:24
  • I will try the latest free jqGrid code. Thanks Oleg. – ivish Feb 17 '16 at 05:20
  • If you're using a newer version of the Gurrido grid, you'll want to use the following in your code: j$.extend({}, j$.jgrid.getRegional(this, "formatter.date"), opts), – Michael Sobczak Aug 02 '17 at 16:37
4

I figured out the solution myself Thanks for the inputs @oleg But the issue was the input being milliseconds and the formatter src format was "U" which is unix and milliseconds/1000 will provide the unit in Unix
So as for now {srcformat: 'U/1000', newformat:'Y/d/m'} gives me the right date format but It doesn't kinda make sense though But still keeping the question open for legitimate answer

thanks