时间戳转化为时间
项目中经常会用到时间戳转化时间等类似的时间操作,自定义一个自己经常使用的方法,很方便
- 方法
// 通过时间戳获取正常的时间显示
function getTime(data,type){
var _data = data;
//如果是13位正常,如果是10位则需要转化为毫秒
if (String(data).length == 13) {
_data = data
} else {
_data = data*1000
}
const time = new Date(_data);
const Y = time.getFullYear();
const Mon = time.getMonth() + 1;
const Day = time.getDate();
const H = time.getHours();
const Min = time.getMinutes();
const S = time.getSeconds();
//自定义选择想要返回的类型
if(type=="Y"){
return `${Y}-${Mon}-${Day}`
}else if(type=="H"){
return `${H}:${Min}:${S}`
}else{
return `${Y}-${Mon}-${Day} ${H}:${Min}:${S}`
}
}
- 调用:
getTime(1561516843837,‘Y’) - 显示结果:
万般滋味,都是生活,公众号求关注哦!