问题
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
回答1:
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
回答2:
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'
.
来源:https://stackoverflow.com/questions/22335652/converting-the-milliseconds-to-date-format-for-jqgrid