- 常用封裝方法
/**
* splitValue 時間戳 轉(zhuǎn)換
* @returns {string}
*/
export function formatDate(value) {
let date = new Date(value);
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? '0' + MM : MM;
let d = date.getDate();
d = d < 10 ? '0' + d : d;
let h = date.getHours();
h = h < 10 ? '0' + h : h;
let m = date.getMinutes();
m = m < 10 ? '0' + m : m;
let s = date.getSeconds();
s = s < 10 ? '0' + s : s;
return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
}
- 高級方法(時間自由格式輸出)
/**
* splitValue 獲取當前時間
* @param value
* @returns {array}
*/
export function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
(date.getFullYear() + '').substr(4 - RegExp.$1.length)
);
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds(),
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? str : ('00' + str).substr(str.length)
);
}
}
return fmt;
}
export default {
formatDate,
};
// 獲取格式化時間函數(shù)
import {formatDate} from '@/common/filter.js';
filters: {
formatDate(time) {
var date = new Date(time);
return formatDate(date, 'yyyy-MM-dd');
}
},
<div class="time">{{item.XXX| formatDate}}</div>
let nowDate = formatDate(new Date(), 'yyyy-MM-dd hh:mm:ss');