通常情况下后台反馈及我们前端计算的时间都是普通时间戳,转换日期和时间的功能十分常用,
废话不多说,上源码。
function timestampTotime(format,timestamp){
let time=new Date(parseInt(timestamp))
//console.log(time)
let month=time.getMonth()+1;
let day=time.getDate();
//console.log(day+"day")
let h=time.getHours();
//console.log(h+"h")
let m=time.getMinutes();
let s=time.getSeconds();
if(format == "date"){
if(month>=1 && month<=9){
month="0"+month
}
if(day>=1 && day<=9){
day='0'+day
}
return month+"-"+day
}else{
if(h>=0 && h<=9){
h='0'+h
}
if(m>=0 && m<=9){
m='0'+m
}
if(s>=0 && s<=9){
s='0'+s
}
return h+":"+m+":"+s
}
}
例: let thisdate=timestampTotime("date",res.data.data.records[i].reportTime);
求日期 格式 MM-DD
let thistime=timestampTotime("time",res.data.data.records[i].reportTime);
求时间 格式 HH:MM:SS