<script>
// 封装日期函数
function formatDateTime(inputTime) {
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
var h = date.getHours();
h = h < 10 ? "0" + h : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
}
</script>
使用前:
使用后:
部分代码:
<script>
// 封装日期函数
function formatDateTime(inputTime) {
var date = new Date(inputTime);
var y = date.getFullYear();
var m = date.getMonth() + 1;
m = m < 10 ? "0" + m : m;
var d = date.getDate();
d = d < 10 ? "0" + d : d;
var h = date.getHours();
h = h < 10 ? "0" + h : h;
var minute = date.getMinutes();
var second = date.getSeconds();
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
}
//加载all
function load() {
myAjax.get(
"/article/findAll",
(res) => {
//成功查到数据之后先清空表单,防止数据重复追加
$(".tables tbody").empty();
console.log("查询栏目数据成功", res.data);
//遍历rea.data数组追加到tbody中
res.data.forEach((item, index) => {
//动态追加节点,循环几次追加几次
//????????? 描述
// moment(item.publishTime).format("YYYY-MM-DD HH:mm:ss")
var newTr = $(`
<tr>
<td>${index + 1}</td>
<td>${item.title}</td>
<td>${formatDateTime(item.publishTime)}</td>
<td>${item.categoryId}</td>
<td>
<i class="fa fa-pencil" data-item='${JSON.stringify(item)}'></i>
<i class="fa fa-trash" data-id=${item.id}></i>
</td>
</tr>`);
//追加节点
$(".tables tbody").append(newTr);
});
},
(err) => {
console.log("失败", err);
}
);
}
load();
</script>