引子
今天在学习DOM的时候,做一个修改元素的案例,想要做个简单的获取时间的网页,发现在格式化输出时es6版本的JS已经删除了format()函数,没办法,只能自己手写一个。
实现代码
function JoinZero(num) {
return num < 10 ? '0' + num : num;
}
function getTime(date) {
var time = (date == null ? new Date() : new Date(date));
let y = time.getFullYear();
let m = JoinZero(time.getMonth() + 1);
let d = JoinZero(time.getDate());
let h = JoinZero(time.getHours());
let mi = JoinZero(time.getMinutes());
let s = JoinZero(time.getSeconds());
return `${y}-${m}-${d}-${h}-${mi}-${s}`;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)