1、先創建一個js文件,複製以下內容保存:
function parseToDate(value) { if (value == null || value == '') { return undefined; } var dt; if (value instanceof Date) { dt = value; } else { if (!isNaN(value)) { dt = new Date(value); } else if (value.indexOf('/Date') > -1) { value = value.replace(/\/Date\((-?\d+)\)\//, '$1'); dt = new Date(); dt.setTime(value); } else if (value.indexOf('/') > -1) { dt = new Date(Date.parse(value.replace(/-/g, '/'))); } else { dt = new Date(value); } } return dt; } // 爲Date類型拓展一個format方法,用於格式化日期 Date.prototype.format = function(format) // author: meizz { 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 formatDateBoxFull(value) { if (value == null || value == '') { return ''; } var dt = parseToDate(value); return dt.format("yyyy-MM-dd hh:mm:ss"); } function formatDatebox(value) { if (value == null || value == '') { return ''; } var dt = parseToDate(value);// 關鍵代碼,將那個長字符串的日期值轉換成正常的JS日期格式 return dt.format("yyyy-MM-dd"); // 這裏用到一個javascript的Date類型的拓展方法,這個是自己添加的拓展方法,在後面的步驟3定義 } //重寫datagrid方法,使datagrid行編輯時,日期控件內的時間格式正確顯示: $.extend($.fn.datagrid.defaults.editors, { datebox : { init : function(container, options) { var input = $('<input type="text">').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); } }, datetimebox : { init : function(container, options) { var input = $('<input type="text">').appendTo(container); input.datetimebox(options); return input; }, destroy : function(target) { $(target).datetimebox('destroy'); }, getValue : function(target) { return $(target).datetimebox('getValue'); }, setValue : function(target, value) { $(target).datetimebox('setValue', formatDateBoxFull(value)); }, resize : function(target, width) { $(target).datetimebox('resize', width); } } });
2、引用這個js文件
<script language="JavaScript" src="xxxx.js"></script>
3、
這樣就可以啦~~~~