时间戳转时间(日期)、时间(日期)转时间戳两种

方法一:
调用: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  弥留残荷 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/mlch/p/10727860.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值