1、下载依赖

npm install moment --save

2、引入依赖

import moment from 'moment';

3、写入methods

//过滤秒:格式化时间
time (value)
{
  return moment(value).format('YYYY-MM-DD HH:mm:ss');
},

一般来说 用这一种方法就够了

下面是整合的各种函数去使用

4、封装成js直接使用(下次直接引入即可)

import moment from 'moment';

// 短时间
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;
}

5、使用方法

引入js

import * as timeFormat from "../../../base/api/timeFormat";

在表格中使用

<el-table-column prop="createTime" label="创建时间" min-width="180">
  <template slot-scope="scope">
      {{leaveTime(scope.row.createTime)}}
    </template>
</el-table-column>

在timePicker中使用(无需引入对应方法)

 <el-date-picker
  v-model="filedParams.payStartDate"
  type="date"
  placeholder="请选择时间"
  value-format="yyyy-MM-dd"
  style="float: left; width: 48%"
>
</el-date-picker>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐