获取前一周期日期

// 同环比获取上一阶段日期处理
export const getNextDate = (date: any, type?: any) => {
if (!date) return;
if (date.length === 8) {
 let newDate = date.slice(0,4)+'-'+date.slice(4);
newDate = newDate.slice(0,7)+'-'+newDate.slice(7);
  const dd = new Date(newDate);
let day = 0;
if (type === 'WOW') {
day = -7;
} else if (type === 'DOD') {
day = -1;
}
  dd.setDate(dd.getDate() + day);
  let y = dd.getFullYear();
  let m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
if (type === 'MOM') {
 m = Number(m) - 1;
if(m === 0) {
m = 12;
y = y - 1;
}
} else if (type === 'QOQ') {
 m = Number(m) - 3;
if(m <= 0) {
m = 12 + m;
y = y - 1;
}
} else if (type === 'YOY') {
y = y - 1;
}
return y + "-" + m + "-" + d;
} else if (date.length === 6) {
let y = date.slice(0,4);
let z = date.slice(4);
if (type === 'WOW') {
z = Number(z) - 1;
if(z === 0) {
z=53;
y = Number(y)-1;
}
} else if (type === 'YOY') {
y = Number(y)-1;
} else if (type === 'MOM') {
z = Number(z) - 1;
if(z === 0) {
z = 12;
y = Number(y)-1;
}
} else if (type === 'QOQ') {
z = Number(z) - 1;
if(z === 0) {
z = 4;
y = Number(y)-1;
}
}
if (z < 10) {
z = '0'+ z;
}
return `${y}${z}`
}
return `${Number(date)-1}`
};
posted @ 2021-01-08 14:15  lipu1993  阅读(113)  评论(0编辑  收藏  举报