沒看到是我js文件裏面有人寫好了 format方法,抱歉。
後臺直接將java.util.date類型傳過來就行,在前臺會變成時間戳的樣子,
在前臺直接用 js new date(後臺傳過來的參數) 就能生成 js的date類型了,
如果想要顯示成 字符串形式 使用js的格式化就行了
new Date(date).Format("yyyy-MM-dd");
格式化方式還有很多種,大家再去百度吧,
ajax請求後臺,後臺直接將java.util.date 類型的date返回,前臺就直接這樣用就行,下面是代碼
function asynAjax() {
$.ajax({
url : '/test/getDate',
type : 'post',
dataType : 'json',
success : function(date) {
new Date(date).Format("yyyy-MM-dd")
}
});
}
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;
}