bootstrap table中时间戳转为datetime格式显示

最近在做项目用的是bootstrap框架,有遇到了数据类型转换的问题,数据库字段用的是时间戳类型,在数据库显示出来是年月日时分秒的形式,而在页面上显示却是一串很长的数字,这个时候别慌,一招解决,超简单~

 

页面显示:

bootstrap table中时间戳转为datetime格式显示_第1张图片

解决办法:

bootstrap table中时间戳转为datetime格式显示_第2张图片

代码片段1:

 { field:'reserveTime',title:'入驻时间',align: 'center',valign: 'middle',
                sortable: true,
                //——修改——获取日期列的值进行转换
                formatter: function (value, row, index) {
                    return changeDateFormat(value)
                }

bootstrap table中时间戳转为datetime格式显示_第3张图片

代码片段2:

//转换日期格式(时间戳转换为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;
    }
}

就这样加上相关代码就可以了,重启一下服务器就行了,亲测有效。

如果只想要显示年月日只需要在最后的返回值中把所得的时分秒的相关代码删除即可。

 

你可能感兴趣的:(bootstrap table中时间戳转为datetime格式显示)