修正前1234567891234
后端修正

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
startdate数据库字段类型为datetime
但是也有不适用的情况
这种加注解的方法不适用于java实体类名称和数据库字段名不一致的情况下,但是可以使用前端修正
前端修正
显示效果为2020-3-25 0:0:0
function timestampToTime(timestamp) { //将时间戳转化为日期显示
var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':';
m = date.getMinutes() + ':';
s = date.getSeconds();
return Y + M + D + h + m + s;
}
fetch('http://localhost:8080/CA/priceRecords').then(response => response.json()).then(json => {
var arr = json.rows;
for (var i = 0; i < arr.length; i++) {
arr[i].endDate = timestampToTime(arr[i].endDate);//时间戳转化
}
this.items = arr;
this.pages = json.pages;
this.page = json.page;
console.log(this.items);
})
显示效果为2020-3-25 00:00:00
function timestampToTime(timestamp) { //将时间戳转化为日期显示
var date = new Date(timestamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000
Y = date.getFullYear() + '-';
M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
D = date.getDate() + ' ';
h = date.getHours() < 10 ? ('0' + date.getHours() + ':') : (date.getHours() + ':');
m = date.getMinutes() < 10 ? ('0' + date.getMinutes() + ':') : (date.getMinutes() + ':');
s = date.getSeconds() < 10 ? ('0' + date.getSeconds()) : date.getSeconds();
return Y + M + D + h + m + s;
}
fetch('http://localhost:8080/CA/priceRecords').then(response => response.json()).then(json => {
var arr = json.rows;
for (var i = 0; i < arr.length; i++) {
arr[i].endDate = timestampToTime(arr[i].endDate);//时间戳转化
}
this.items = arr;
this.pages = json.pages;
this.page = json.page;
console.log(this.items);
})
本文探讨了在前后端开发中处理时间戳和日期格式化的问题,包括使用@JsonFormat注解在后端进行时间格式化,以及在前端通过JavaScript函数实现时间戳到日期的转换。提供了具体代码示例,展示了如何确保时间和日期在展示时符合预期格式。
470

被折叠的 条评论
为什么被折叠?



