// 格式化时间
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 formatDate = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
return [year, month, day].map(formatNumber).join('-')
}
// 处理数字格式
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
// 将formatTime与formatDate导出
module.exports = {
formatTime:formatTime,
formatDate:formatDate
}
格式化日期与时间
最新推荐文章于 2022-04-05 20:01:57 发布