js格式转换
1.中国标准时间转换为正常格式:
let date = this.start_timestamp;
let date_value = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
转换后格式为:2020-6-20 10:00:00
将时间转换为时间戳 :
let millisecond = Date.parse(date_value);
2.封装调用(当后台传递过来时间戳形式如:1592589641 这样的形式)
// 时间戳转换
export function formatDateTime(date, fmt) {
if (!date) {
return ''
}
date *= (date.toString().length === 10 ? 1000 : 1)
let _date = new Date(date)
let _fmt = fmt || 'yyyy-MM-dd hh:mm:ss'
let o = {
'M+': _date.getMonth() + 1,
'd+': _date.getDate(),
'h+': _date.getHours(),
'm+': _date.getMinutes(),
's+': _date.getSeconds()
}
if (/(y+)/.test(_fmt)) {
_fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length))
}
for (let 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
}
调用HTML中
<el-table-column label="活动购买时间(区间)" align="center">
<template slot-scope="scope">{{scope.row.start_time | formatTime}} - {{scope.row.end_time | formatTime}}</template>
</el-table-column>
<el-table-column label="活动核销时间(区间)" align="center">
<template slot-scope="scope">{{scope.row.use_start_time | formatTime }} - {{scope.row.use_end_time | formatTime }}</template>
js部分
filters:{
formatTime(time) {
console.log(time);
if (time == null || time === '') {
return 'N/A';
}
return formatDateTime(time, 'yyyy-MM-dd hh:mm:ss')
},
},