2011-12-01 28 views
2

我使用的jqGrid和我的格子的定義是這樣的:的jqGrid不排序當顯示大紀元時間(自爲毫秒)的日期

... 
colNames:['Type','Date','Message','User Name','Host'], 
colModel:[{name:'type',index:'type', width:100}, 
{name:'date',index:'date', sorttype:'date', formatter:'date', 
    formatoptions: {newformat:'d-M-Y'}, width:100}, 
{name:'log',index:'log', width:200}, 
{name:'username',index:'username', width:50}, 
{name:'host',index:'host', width:50}], 
... 

當調試我的未來數據的日期值之一(它是Number)如下:

1322550786997 

窗格顯示它這樣:

29-Nov-2011 

一切OK,直到這一點。但是,當我想對日期列進行排序時,它不會改變任何內容。

任何想法?

+0

哪種數據類型擁有1322550786997?它是「字符串」還是「數字」? – Oleg

+0

@ Oleg它是數字。 – kamaci

回答

4

問題是解碼的Unix(formatoptions: {srcformat: 'U', newformat: 'd-M-Y'})日期1322550786997得到我們19-Dec-43879而不是29-Nov-2011。您更正日期的表示形式將是字符串"\/Date(1322550786997)\/"而不是數字1322550786997

the demo

enter image description here

修訂:您還可以使用下面的自定義格式作爲一種解決方法

formatter: function (cellval, opts) { 
    var date = new Date(cellval); 
    opts = $.extend({}, $.jgrid.formatter.date, opts); 
    return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts); 
} 

它創建Date然後用原date格式將其轉換格式爲'd-M-Y'。請參閱here演示。

+0

感謝您的幫助,但我想我錯過了什麼。我從Java獲得時間,我認爲從時代開始的時間爲毫秒。 – kamaci

+0

@kamaci:如果您從Java發佈日期,您應該更好地使用ISO 8601日期格式(請參閱[這裏](http://en.wikipedia.org/wiki/ISO_8601)和[這裏](http:// www。 w3.org/TR/NOTE-datetime))。它是jqGrid使用的標準輸入格式。 – Oleg

+0

@kamaci:您可以使用我在我的答案的** UPDATED **部分中描述的解決方法。 – Oleg

相關問題