/**
* 获取当前星期的起始日期和结束日期
* @param {string} startFormat 周一的时间格式
* @param {string} endFormat 周日的时间格式
* @param {number} timestamp 所在周的时间戳,若不传入,则默认使用当前时刻的时间戳
* @returns {string, string} {startDate, endDate} 返回的数据
*/
export const getWeekStartAndEnd = (
startFormat: string,
endFormat: string,
timestamp?: number
): {
startDate: string,
endDate: string,
} => {
const oneDayTime = 1000 * 3600 * 24;
const nowDate = timestamp ? new Date(timestamp) : new Date();
const now = nowDate.getTime();
const nowDay = nowDate.getDay() === 0 ? 7 : nowDate.getDay();
const startDate = new Date(now - oneDayTime * (nowDay - 1));
const endDate = new Date(now + oneDayTime * (7 - nowDay));
return {
startDate: formatTime(startDate.getTime(), startFormat),
endDate: formatTime(endDate.getTime(), endFormat),
};
};