easyui datagrid 日期時間顯示不正常,
原因: 後臺Java 類型爲 DATE 經過JSON工具轉化後傳到前臺來就亂了
兩種思路: 1: 還沒進行json處理之前就給專程string
2. 到了前臺再轉回來。
解決方案一:
- {field:'issueTime',title:'生效時間',
- formatter:function(value,row,index){
- var unixTimestamp = new Date(value);
- return unixTimestamp.toLocaleString();
- }
-
},
解決方案二:
爲了克服數據中時間爲空的情況,我們可以考慮使用前臺js解析返回的json數據。
第一步、我們先定義一個方法,使日期列正常顯示,js代碼如下:
- function formatDatebox(value) {
- if (value == null || value == '') {
- return '';
- }
- var dt;
- if (value instanceof Date) {
- dt = value;
- }
- else {
- dt = new Date(value);
- if (isNaN(dt)) {
- value = value.replace(/\/Date(−?\d+)\//, '$1'); //標紅的這段是關鍵代碼,將那個長字符串的日期值轉換成正常的JS日期格式
- dt = new Date();
- dt.setTime(value);
- }
- }
- return dt.format("yyyy-MM-dd"); //這裏用到一個javascript的Date類型的拓展方法,這個是自己添加的拓展方法,在後面的步驟3定義
- }
- $.extend(
- $.fn.datagrid.defaults.editors, {
- datebox: {
- init: function (container, options) {
- var input = $('').appendTo(container);
- input.datebox(options);
- return input;
- },
- destroy: function (target) {
- $(target).datebox('destroy');
- },
- getValue: function (target) {
- return $(target).datebox('getValue');
- },
- setValue: function (target, value) {
- $(target).datebox('setValue', formatDatebox(value));
- },
- resize: function (target, width) {
- $(target).datebox('resize', width);
- }
- }
- });
- Date.prototype.format = function (format)
- {
- var o = {
- "M+": this.getMonth() + 1, //month
- "d+": this.getDate(), //day
- "h+": this.getHours(), //hour
- "m+": this.getMinutes(), //minute
- "s+": this.getSeconds(), //second
- "q+": Math.floor((this.getMonth() + 3) / 3), //quarter
- "S": this.getMilliseconds() //millisecond
- }
- if (/(y+)/.test(format)) format = format.replace(RegExp.$1,
- (this.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (var k in o) if (new RegExp("(" + k + ")").test(format))
- format = format.replace(RegExp.$1,
- RegExp.$1.length == 1 ? o[k] :
- ("00" + o[k]).substr(("" + o[k]).length));
- return format;
- }
- function initTable(queryData) {
- $('#test').datagrid({ //定位到Table標籤,Table標籤的ID是test
- fitColumns: true,
- url: '/OperationLog/QueryLog', //指向後臺的Action來獲取當前用戶的信息的Json格式的數據
- title: '日誌信息查詢', //標識
- iconCls: 'icon-save',
- height:335,
- nowrap: true,
- autoRowHeight: false,
- striped: true,
- collapsible: true,
- pagination: true,
- rownumbers: true,
- //sortName: 'ID', //根據某個字段給easyUI排序
- //sortOrder: 'asc',
- remoteSort: false,
- //idField: 'number',
- queryParams: queryData, //異步查詢的參數
- columns: [[
- { title: '操作人', field: 'userID',sortable: true,},
- { title: '操作類型', field: 'operatorType', sortable: true, },
- { title: '檔案人姓名', field: 'userName', sortable: true, },
- { title: '操作時間', field: 'operatorTime', formatter: formatDatebox, editor: 'datebox', sortable: true, width: 10, }
- ]],
- });
- }
-
</script>
我最後是這麼做的:
{field:'kkny',title:'開考年月',width:150,formatter:function(value,row,index){
var unixTimestamp = new Date(value);
return unixTimestamp.toLocaleString();
}}
function datetoLocal(value){
var unixTimestamp = new Date(value);
var year = unixTimestamp.getFullYear();
var month= unixTimestamp.getMonth()+1;
var day = unixTimestamp.getDate()+1;
var hours = unixTimestamp.getHours()+1 ;
var minutes = unixTimestamp.getMinutes()+1 ;
var seconde = unixTimestamp.getSeconds()+1;
var result = year+"-"+month+"-"+day +" "+hours+":"+minutes+":"+seconde;
return result;
}