在java spring mvc 開發過程中,通過json 格式,向前端傳遞數據,日期格式發生了轉變,
在前台數據展示時,要進行一定格式的轉換才能正常顯示;
我在開發中使用了easy ui 和my 97的日歷控件,探索良久,得以解決。主要方法如下:
1、擴展new date()的顯示格式,函數如下:
// 對Date的擴展,將 Date 轉化為指定格式的String // 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 可以用 1-2 個占位符, // 年(y)可以用 1-4 個占位符,毫秒(S)只能用 1 個占位符(是 1-3 位的數字) Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getMonth() + 1, //月份 "d+": this.getDate(), //日 "h+": this.getHours(), //小時 "m+": this.getMinutes(), //分 "s+": this.getSeconds(), //秒 "q+": Math.floor((this.getMonth() + 3) / 3), //季度 "S": this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; }
(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") //輸出結果: 2018-02-03 08:36:10.400 (new Date()).Format("yyyy-M-d h:m:s.S") //輸出結果: 2018-2-03 9:36:35.572
實際應用例子如下: