时间格式化的处理

6 篇文章 0 订阅

使用moment   ,查看文档地址 http://momentjs.cn/

1)下载moment   npm i moment

2)有些地方需要用到时间格式化 ,需要处理数据

我比较喜欢的做法有两种

方法有两种:

方法1.对定义的currentTime时间格式化

function formatTime(arr) {
    arr.forEach(item => {
        item.currentTime = moment(item.currentTime).format("YYYY年MM月DD日");
    });
    return arr;
}

 

getAllUsers(params).then((res) => {    //这个方法是接口数据        
        // console.log(res);          
        // 对数组中时间格式化
        this.tableData = formatTime(res.data.users);               
        this.total = res.data.total; //一共多少条数据
      });

 

方法2:封装成过滤器   在main.js引入

import Vue rom "vue";
import moment from "moment";
Vue.filter("formatTime", function(time) {
    return moment(time).format("YYYY-MM-DD");
});

在页面上应用

<template slot-scope="scope">{{scope.row.currentTime | formatTime}}</template>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值