23 lines
1.5 KiB
TypeScript
23 lines
1.5 KiB
TypeScript
export function previousCompleteMonth(reference = new Date()): { year: number; month: number } {
|
|
const previous = new Date(reference.getFullYear(), reference.getMonth() - 1, 1);
|
|
return { year: previous.getFullYear(), month: previous.getMonth() + 1 };
|
|
}
|
|
|
|
export function defaultQueryMonth(options?: { periods?: string[]; latestDataPeriod?: { year?: number; month?: number } } | null, reference = new Date()): { year: number; month: number } {
|
|
const previous = previousCompleteMonth(reference);
|
|
const key = `${previous.year}-${String(previous.month).padStart(2, '0')}`;
|
|
const latest = options?.latestDataPeriod;
|
|
const latestKey = latest?.year && latest.month && latest.month >= 1 && latest.month <= 12
|
|
? `${latest.year}-${String(latest.month).padStart(2, '0')}` : undefined;
|
|
const previousMissing = Array.isArray(options?.periods)
|
|
? !options.periods.includes(key) : latestKey != null && latestKey < key;
|
|
// 只按已发布月份判断;筛选结果为空不会改动用户选定的月份。
|
|
return previousMissing ? previousCompleteMonth(new Date(previous.year, previous.month - 1, 1)) : previous;
|
|
}
|
|
|
|
export function previousCompleteQuarter(reference = new Date()): { year: number; quarter: number } {
|
|
const firstMonthOfCurrentQuarter = Math.floor(reference.getMonth() / 3) * 3;
|
|
const previousQuarterEnd = new Date(reference.getFullYear(), firstMonthOfCurrentQuarter, 0);
|
|
return { year: previousQuarterEnd.getFullYear(), quarter: Math.floor(previousQuarterEnd.getMonth() / 3) + 1 };
|
|
}
|