js时间处理
日期格式转换
const formatTime = (date,j='/',l='Ymd H:i:s') => {
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()
if(l == 'Ymd'){
return [year, month, day].map(formatNumber).join(j)
}else{
return [year, month, day].map(formatNumber).join(j) + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
时间戳转日期
时间戳转日期
const nToDate = (timestamp,j = '/',l='Ymd') => {
if(timestamp.length == 10){
timestamp = timestamp*1000;
}
let date = new Date(timestamp);
return formatTime(date,j,l)
}
时间戳转日期时间
const nToDateTime = (timestamp,j = '/') => {
if(timestamp.length == 10){
timestamp = timestamp*1000;//时间戳为10位需*1000,时间戳为13位的话不需乘1000
}
var date = new Date(timestamp);
return formatTime(date,j)
}