一、
/**
* 时间戳转时间
* @param timeNumber : number
* @param {boolean} [onlyDate=false]
* @return {*} : string
*/
export const numberToTime = function (timeNumber, onlyDate) {
function toDou (iNow) {
return iNow < 10 ? '0' + iNow : '' + iNow;
}
if (+timeNumber !== 0) {
let oDate = new Date();
if (timeNumber !== undefined) {
oDate.setTime(timeNumber * 1000);
}
let date = oDate.getFullYear() + '-' + toDou(oDate.getMonth() + 1) + '-' + toDou(oDate.getDate());
if (onlyDate) {
return date;
}
return date + ' ' + toDou(oDate.getHours()) + ':' + toDou(oDate.getMinutes()) + ':' + toDou(oDate.getSeconds());
} else {
// 如果后台传过来是 0,即没有这个时间
return ' - ';
}
};
二、
// 格式化日期,如月、日、时、分、秒保证为2位数
formatNumber (n) {
n = n.toString()
return n[1] ? n : '0' + n
},
// 参数number为毫秒时间戳,format为需要转换成的日期格式
formatTime (number, format) {
let time = new Date(number)
let newArr = []
let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
newArr.push(time.getFullYear())
newArr.push(this.formatNumber(time.getMonth() + 1))
newArr.push(this.formatNumber(time.getDate()))
newArr.push(this.formatNumber(time.getHours()))
newArr.push(this.formatNumber(time.getMinutes()))
newArr.push(this.formatNumber(time.getSeconds()))
for (let i in newArr) {
format = format.replace(formatArr[i], newArr[i])
}
return format
},
var time = _this.formatTime(new Date(), 'Y-M-D h:m:s')