做crm项目里 一个界面需要把数据库数字转换成日期格式 今天我就把这个方法写出来
第一种写法:在那个时间里调用下面写的时间转换方法 就可以把某一个字段变成标准的时间格式!
$(function(){
// var $=jQuery=layui.jquery;
layui.use([ 'table', 'form','layer' , 'element'], function() {
var table = layui.table;
var form = layui.form;
var layer = layui.layer;
var element = layui.element;
// 第一个实例
table.render({
elem : '#test',//页面元素
height : 512,
url : pro_Url+'/sal/ChanceAction_listChance', // 数据接口
// defaultToolbar : [ 'filter', 'print', 'exports' ],
page : true ,// 开启分页
limit : 10,
totalRow: true, //开启合计行
//limits : [ 5, 10 ], // 控制多少行一页(默认五条一页)
// cellMinWidth : 80,
cols : [ [
{type : "checkbox",fixed : true},
{field : 'chc_id', align : 'center',title : '编号',width : 100,sort : true},
{field : 'chc_cust_name',title : '客户名称',align : 'center',width : 180},
{field : 'chc_title',title : '概要',width : 240},
{field : 'chc_linkman',title : '联系人',align : 'center',width : 150},
{field : 'chc_tel',title : '联系电话',align : 'center',width : 215,sort : true},
{field : 'chc_create_date',title : '创建时间',align : 'center',width : 240,
templet: '<div>{{ layui.laytpl.toDateString(d.chc_create_date) }}</div>'},
{fixed : 'right',width : 250,align : 'center',toolbar : '#barDemo'}
] ],
id : 'pushReload'//表格id
});//结束table
var $ = layui.$, layer = layui.layer, active = {
// 模糊查询的页面重载
reload : function() {//拿到页面每一个值
var chc_cust_name = $('#custname').val();
var chc_title = $('#title').val();
var chc_linkman = $('#linkman').val();
// 执行重载
table.reload('pushReload', {
page : {
curr : 1// 重新从第 1 页开始
},
where : {//传的参数到后台
chc_cust_name : chc_cust_name,
chc_linkman : chc_linkman,
chc_title : chc_title
}
});
}
};//重载结束
// 查询按钮实现
$('.demoTable #selectBtn').on('click', function() {
var type = $(this).data('type');
active[type] ? active[type].call(this) : '';
});
//时间戳的处理
layui.laytpl.toDateString = function(d, format){
var date = new Date(d || new Date())
,ymd = [
this.digit(date.getFullYear(), 4)
,this.digit(date.getMonth() + 1)
,this.digit(date.getDate())
]
,hms = [
this.digit(date.getHours())
,this.digit(date.getMinutes())
,this.digit(date.getSeconds())
];
format = format || 'yyyy-MM-dd HH:mm:ss';
return format.replace(/yyyy/g, ymd[0])
.replace(/MM/g, ymd[1])
.replace(/dd/g, ymd[2])
.replace(/HH/g, hms[0])
.replace(/mm/g, hms[1])
.replace(/ss/g, hms[2]);
};
//数字前置补零
layui.laytpl.digit = function(num, length, end){
var str = '';
num = String(num);
length = length || 2;
for(var i = num.length; i < length; i++){
str += '0';
}
return num < Math.pow(10, length) ? str + (num|0) : num;
};
第二种方法:也是写一个方法
//时间戳
function createTime(v){
var date = new Date(v);
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 M = date.getMinutes();
M = M<10?("0"+M):M;
var str = y+"-"+m+"-"+d+" "+h+":"+M;
return str;
}
cols : [ [
{type : "checkbox",fixed : true},
{field : 'chc_id', align : 'center',title : '编号',width : 100,sort : true},
{field : 'chc_cust_name',title : '客户名称',align : 'center',width : 150},
{field : 'chc_title',title : '概要',width : 190,align : 'center'},
{field : 'chc_linkman',title : '联系人',align : 'center',width : 150},
{field : 'chc_tel',title : '联系电话',align : 'center',width : 180,sort : true},
{field : 'chc_create_date',title : '创建时间',align : 'center',width : 200,
templet: function (row){
return createTime(row.chc_create_date);
}
},
然后也可以像那样展示出来