/**
* @export tsx
* @param {*} tiem 传入时间 new Date() 和时间戳 不能传 0和1 其他的默认 当前时间
* @param {*} format 需要的格式
* @param {*} delay // 推前几天 就传几 0是当前传入的时间
*/
export function timeTransformation(tiem, format, delay) {
if (!delay) {
delay = 0;
}
if (tiem) {
let date = new Date();
if (tiem instanceof Date) {
date = new Date(tiem.getTime() - 24 * delay * 60 * 60 * 1000);
} else {
if (tiem.toString().length == 10) {
date = new Date(parseFloat(tiem) * 1000 - 24 * delay * 60 * 60 * 1000);
} else if (tiem.toString().length == 13) {
date = new Date(parseFloat(tiem) - 24 * delay * 60 * 60 * 1000);
}
}
const year = date.getFullYear();
const month =
date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1;
const strDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
const hours =
date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
const minutes =
date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
const seconds =
date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
if (format == "1") {
const obj = year + "-" + month + "-" + strDate;
return obj;
} else if (format == "2") {
const obj = hours + ":" + minutes;
return obj;
} else if (format == "3") {
const obj =
year +
"-" +
month +
"-" +
strDate +
" " +
hours +
":" +
minutes +
":" +
seconds;
return obj;
} else return "";
} else return "";
}