偶尔看了下微信小程序,发现里面的时间格式化代码不错,比自己写的要简洁点,学习无处不在先保存下 格式如:2018/02/01 13:52:23 javascript 代码 const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':') } const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } console.log(formatTime(new Date()));
评论 (0 )
最新评论
暂无评论
赶紧努力消灭 0 回复