1. 程式人生 > 其它 >element ui格式化表格裡面的時間

element ui格式化表格裡面的時間

element ui格式化表格裡面的時間

原文連結:https://blog.csdn.net/weixin_43503080/article/details/106136068

1.安裝monet.js庫

npm install moment --save

2.全域性引入

import moment from 'moment';

3.method:定義函式進行時間的格式化

    //過濾秒:格式化時間
    leaveTime(value)
    {
      return moment(value).format('YYYY-MM-DD HH:mm');
    },

4.格式element中的時間欄位

        <el-table-column label="建立時間" prop="CreatedAt">
          <template v-slot="scope">
            {{leaveTime(scope.row.CreatedAt)}}
          </template>
        </el-table-column>
      </el-table>

附錄其他使用程式碼

// 短時間
export const shortTime = function (value) {
    return moment(value).format('YYYY-MM-DD');
}

// 長時間
export const time = function (value) {
    return moment(value).format('YYYY-MM-DD HH:mm:ss');
}

//過濾秒
export const leaveTime = function (value) {
    return moment(value).format('YYYY-MM-DD HH:mm');
}

// 年月
export const monthTime = function (value) {
    return moment(value).format('YYYY-MM');
}

// 時分秒
export const secondsTime = function (value) {
    return moment(value).format('HH:mm:ss');
}

// 中國標準時間的轉化
export const filterTime = (time, type = 'short') => {
    if (type == 'short') {
        return moment(time).format('YYYY-MM-DD')
    } else {
        return moment(time).format('YYYY-MM-DD HH:mm:ss')
    }
}

export const startOfDate = function(d, dateType = 'day'){
    return moment(d).startOf(dateType)
}

export const endOfDate = function(d, dateType = 'day'){
    return moment(d).endOf(dateType)
}

// 當月第一天和最後一天   傳入一個日期,返回陣列['2019-12-01','2019-12-31']
export const lastDateofMonth = function (d) {
    let firstDate = moment(d).startOf('month').format('YYYY-MM-DD');
    let endDate = moment(d).endOf('month').format('YYYY-MM-DD');
    let Datearr = [];
    Datearr.push(firstDate);
    Datearr.push(endDate);
    return Datearr;
}