vue js 时间格式转换

用需求分析:前台页面有时需要展示YYYY-MM-DD格式,但后台却返回给我们YYYY-MM-DD hh:mm:ss、或者是一串字符

一、通用的时间转换方法如下:

//格式化处理
            dateFormat(time) {
                let date = new Date(time);
                let year = date.getFullYear();
                // 在日期格式中,月份是从0开始的,因此要加0,使用三元表达式在小于10的前面加0,以达到格式统一  如 09:11:05
                let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                let hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                let minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                let seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                // 拼接
                // return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
                return year + "-" + month + "-" + day;
            },

页面使用如下:

<view v-if="item.rukudate">{{ dateFormat(item.rukudate) }}</view>

二、过滤器

思路:在过滤中写一个时间处理方法,分别选取年月日,月份和日期需要01 02这种展示效果,需要用toString转化截图前两项,然后拼接返回

// 时间过滤器
        filters:{
            formatDate(date){
                console.log(date)
                let newDate = new Date(date);
                let year = newDate.getFullYear();
                let month = newDate.getMonth().toString().padStart(2,0);
                let day = newDate.getDay().toString().padStart(2,0);
                return year + '-' + month + '-' + day;
            }
        },

页面引用如下

<view>发表时间:{{ item.add_time | formatDate }}</view>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值