弥留残荷

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
方法一:
调用:common.dateTurnTime(record.createTime);
common.dateTurnTime= (list,path = 'YYYY-MM-DD') => { // 时间戳转时间
let time = parseInt(list);
let date = new Date(time);
let Y = date.getFullYear() + '-';
let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) ;
let m = ':' + (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) ;
let s = ':' + (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
let result ;
if(path.indexOf('Y') > -1) result = Y;
if(path.indexOf('M') > -1) result += M;
if(path.indexOf('D') > -1) result += D;
if(path.indexOf('h') > -1) result +=h;
if(path.indexOf('m') > -1) result +=m;
if(path.indexOf('s') > -1) result +=s;
// return Y + M + D + h + m + s;
return result;
};
//日期转时间戳
common.timeToDate = function (timestamp) {
let date = new Date(timestamp);
let time = Date.parse(date);
return time;
};
方法二:
调用:new Date(text).format('s')
/**
* 日期格式化
* @param type
* @returns {string}
*/
Date.prototype.format = function (type) {
var y = this.getFullYear(),
m = this.getMonth() + 1,
d = this.getDate(),
h = this.getHours(),
mm = this.getMinutes(),
ss = this.getSeconds();
m = m <= 9 ? '0' + m : m;
d = d <= 9 ? '0' + d : d;
h = h <= 9 ? '0' + h : h;
mm = mm <= 9 ? '0' + mm : mm;
ss = ss <= 9 ? '0' + ss : ss;
if (type === 'yyyy-mm-dd') {
return y + "-" + m + "-" + d;
}
if (type === 'yyyy年mm月') {
return y + "年" + m + "月";
}
if (type === 's') {
return y + "-" + m + "-" + d + " " + h + ":" + mm;
}
if (type === 'mm-dd hh:mi') {
return m + "-" + d + " " + h + ":" + mm;
}
if (type === 'hh:mi:ss') {
return h + ":" + mm + ":" + ss;
}
return y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + ss;
};
posted on 2019-04-18 10:02  弥留残荷  阅读(1053)  评论(0编辑  收藏  举报