Layui 时间转换时间戳
我比较喜欢用Layui,当然也遇到一些坑!例:绑定时间的时候是这种 PS:这里只是其中的一种办法用来页面显示,如果用Layui默认导出Excel,还是一样的时间搓
是这种13位数据的日期,如果不经过转换是不能正常显示的,当然这是我遇到的问题不知道你们有没有遇到,后来我也是经过了解才知道怎么回事!
在表格里调用下列方法就解决了
查询日期为空时,系统会默认一个日期“1-01-01”
上解决方案:
1 //--------------------------------------------开始--------------------------------------- 2 //layui 后台查询出来的时间绑定 时间戳转换 后台返回来的数据都要经过时间戳处理否则显示10或13位数字 3 function Format(datetime) { 4 if (datetime != "") { 5 var date = new Date(parseInt(datetime.replace("/Date(", "").replace(")/", ""), 10)); 6 //月份为0-11所以+1,月份小于10补个0 7 if (date != "") { 8 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; 9 var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); 10 var time = date.getFullYear() + "-" + month + "-" + currentDate; 11 if (time == "1-01-01") { 12 return time = ""; 13 } 14 else { 15 return time; 16 } 17 } 18 } 19 else { 20 return ""; 21 } 22 } 23 //--------------------------------------------end--------------------------------------- 24 25 26 }); 27 28 29
1 //layui 后台查询出来的时间绑定 时间戳转换 后台返回来的数据都要经过时间戳处理否则显示10或13位数字 2 function Format(datetime) { 3 debugger; 4 if (datetime != "") { 5 var date = new Date(parseInt(datetime.replace("/Date(", "").replace(")/", ""), 10)); 6 //月份为0-11所以+1,月份小于10补个0 7 if (date != "") { 8 var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; 9 var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); 10 var time = date.getFullYear() + "-" + month + "-" + currentDate; 11 if (time == "1-01-01") { 12 return time = ""; 13 } 14 else { 15 return time; 16 } 17 } 18 } 19 else { 20 return ""; 21 } 22 }
注意:在数据库字段为空的时候会返回“1-01-01”,必须判断然后给返回“”,页面才会不显示
经过转换后的效果图:
希望能帮助到喜欢用这个ui框架的小伙伴,还有不懂的可以在下方留言哦!当然解决方案还有很多我觉得这个是最适合我自己的,我也是个菜鸟有写的不好的地方也欢迎大家指出及时改正!
一步一个脚印慢慢前行!