需求:后端返回的时间格式可能是data或者时间戳,但是页面需要的是2024-02-03 00:00:00的格式
思路:将获取的时间转换成对应的字符串即可
/**
*
* @param {时间戳或者data格式} data
* @param {1-返回年月日时分秒 2-返回年月日 3-返回年 } type
* @returns
*/
formatTime(data, type = 1) {
let startDate = new Date(data);
const year = startDate.getFullYear();
const month = String(startDate.getMonth() + 1).padStart(2, "0");
const day = String(startDate.getDate()).padStart(2, "0");
const hours = String(startDate.getHours()).padStart(2, "0");
const minutes = String(startDate.getMinutes()).padStart(2, "0");
const seconds = String(startDate.getSeconds()).padStart(2, "0");
switch (type) {
case 1:
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
case 2:
return `${year}-${month}-${day}`;
case 3:
return `${year}`;
}
},