很简单,在我们需要转化的时候直接调用getTime()方法,并传入参数,第一个是我们需要转化的时间戳,第二个参数是我们需要返回的时间格式类型,这里可以传入两个参数Y和H,Y返回年月日2020-10-20号的格式,H返回时分秒23:23:59格式,不传的话返回年月日加时分秒2020-10-20 23:10:58
getTime(data, type){ //data时间戳,type返回的类型默认Y,可传参Y和H
let time = new Date(data);
let Y = time.getFullYear();
let Mon = time.getMonth() + 1;
let Day = time.getDate();
let H = time.getHours();
let Min = time.getMinutes();
let S = time.getSeconds();
//自定义选择想要返回的类型
if(type === "Y") { //返回年月日2020-10-10
return `${Y}-${Mon}-${Day}`
} else if(type === "H") { //返回时分秒20:10:10
return `${H}:${Min}:${S}`
} else { //返回年月日时分秒2020-10-10 10:26:38
return `${Y}-${Mon}-${Day} ${H}:${Min}:${S}`
}
}
创作借鉴:https://blog.csdn.net/baidu_41604826/article/details/93718973