layui表格字段转日期格式

做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;
				};  

				
				

 

64354

第二种方法:也是写一个方法

//时间戳
	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);
		                        }
									},

然后也可以像那样展示出来

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值