tnblog
首页
视频
资源
登录
愿你出走半生,归来仍是少年
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

layui时间格式化

10952人阅读 2020/6/24 10:19 总访问:1635451 评论:0 收藏:0 手机
分类: 前端

可以先尝试这样写:

{field: 'showtime',title: '时间',templet:"<div>{{layui.util.toDateString(d.showtime, 'yyyy-MM-dd HH:mm:ss')}}</div> "


但是这样写很有可能出现:layui时间格式化 NaN-NaN-NaN

这一个时候可以使用自定义函数来解决,返回一个你处理后的时间即可

{ field: 'CreateDateTime', title: '时间', templet: function (data) { return  你处理后的时间 } }

这个函数有个参数,这个参数随便取名字即可,可以返回这一行的数据,使用这个参数点出来即可,比如要取这个时间就是data.CreateDateTime


处里时间的方法:

可以先把/Date(1567138282927)/ 这种格式的时间转化成日期类型,然后在格式


化成日期类型:

new Date(parseInt('/Date(1567138282927)/'.substr(6, 13)));

然后在格式化成指定的日期:

方法可以参考:https://www.tnblog.net/hb/article/details/3676

具体实现:

, {
    field: 'CreateDateTime', title: '时间', templet: function (data) {
       var day = new Date(parseInt(data.CreateDateTime.substr(6, 13)))
       var time = day.format("yyyy-MM-dd hh:mm");
       return time;
    }
}

其中格式化的方法:

/**
 *对Date的扩展,将 Date 转化为指定格式的String
 *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
 *年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 *例子:
 *(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
 *(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
 */
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}




欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价