/** * 根据类型获取日期区间字符串 * @param type '当年' | 'curMonth' | '当日' * @param show 区分实际值还是显示值, true为实际值, false为显示值 * @returns [start, end] 例:['2024-01-01 00:00:00', '2024-12-31 23:59:59'] */ export function getRangeByType(type: string, show: boolean): [string, string] { const now = new Date() const year = now.getFullYear() const month = (now.getMonth() + 1).toString().padStart(2, '0') const day = now.getDate().toString().padStart(2, '0') if (!show) { if (type === 'curYear') { return [ `${year}-01-01 00:00:00`, `${year}-12-31 23:59:59`, ] } if (type === 'curMonth') { const lastDay = new Date(year, now.getMonth() + 1, 0).getDate() return [ `${year}-${month}-01 00:00:00`, `${year}-${month}-${lastDay.toString().padStart(2, '0')} 23:59:59`, ] } if (type === 'curDay') { return [ `${year}-${month}-${day} 00:00:00`, `${year}-${month}-${day} 23:59:59`, ] } } if (show) { if (type === 'curYear') { return [ `${year}-01-01`, `${year}-12-31`, ] } if (type === 'curMonth') { const lastDay = new Date(year, now.getMonth() + 1, 0).getDate() return [ `${year}-${month}-01`, `${year}-${month}-${lastDay.toString().padStart(2, '0')}`, ] } if (type === 'curDay') { return [ `${year}-${month}-${day}`, `${year}-${month}-${day}`, ] } } // 兜底返回空字符串数组 return ['', ''] }