项目用js时间格式化
文章目录
timestampToTime (timestamp, format = 'yyyy-MM-dd HH:mm:ss') {
// timestamp 时间
/*
* format 格式化
* yyyy 年
* M 月 不补0
* MM 月
* d 日 不补0
* dd 日
* H 时 不补0
* HH 时
* m 分 不补0
* mm 分
* s 秒 不补0
* ss 秒
*
* */
function add0 (m) { return m < 10 ? '0' + m : m; }
let time = new Date(timestamp);
let y = time.getFullYear();
let m = time.getMonth() + 1;
let d = time.getDate();
let h = time.getHours();
let mm = time.getMinutes();
let s = time.getSeconds();
return timestamp ? format.replace(/yyyy/g, y).replace(/MM/g, add0(m)).replace(/M/g, m).replace(/dd/g, add0(d)).replace(/d/g, d).replace(/HH/g, add0(h)).replace(/H/g, h).replace(/mm/g, add0(mm)).replace(/m/g, mm).replace(/ss/g, add0(s)).replace(/s/g, s) : '';
}

@Author: luch

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论