您的位置:首页 > Web前端 > BootStrap

bootstrap Table将时间戳改为日期格式

2019-07-31 15:59 2141 查看
原文链接:https://www.geek-share.com/detail/2723395220.html

一、遇到的问题

从mybatis中获取的是时间格式:2019-07-31 10:07:33,但是传到前台得到的确实时间戳

二、解决方法

  1. 在JS中添加如下代码
{
title: '创建时间',
field: 'cDate',
align: 'center',
//获取日期列的值进行转换
formatter: function (value, row, index) {
return changeDateFormat(value)
}
},
  1. 新增 changeDataFormat方法:
//转换日期格式(时间戳转换为datetime格式)
function changeDateFormat(cellval) {
var dateVal = cellval + "";
if (cellval != null) {
var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

return date.getFullYear() + "-" + month + "-" + currentDate + " " + hours + ":" + minutes + ":" + seconds;
}
}

文章转载自:博客园·小葱拌豆腐~

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: