頁面從資料庫獲取時間顯示有誤
阿新 • • 發佈:2019-01-04
1.在jsp頁面時間顯示為一串數字,不符合正常格式
1)可以新增taglib
<%@ taglib prefix=”fmt” uri=”http://java.sun.com/jsp/jstl/fmt”%>
使用的時候需要匯入包,jstl和standard這兩個包
然後在顯示時間的地方加入fmt標籤,例如:
<input type="text" id="date" value="<fmt:formatDate value="{row.addtime}" pattern="yyyy-MM-dd"/>"/>
2)在js檔案裡進行時間格式處理
function formatDatebox(value) {
if (value == null || value == '') {
return '';
}
var dt;
if (value instanceof Date) {
dt = value;
} else {
dt = new Date(value);
}
return dt.format("yyyy-MM-dd"); //擴充套件的Date的format方法(上述外掛實現)
}
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()
}
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;
}
呼叫formatDatebox(time)函式即可