From c3a880e5f53df5dcb1e068f2a8bce6ff6594de39 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 5 Nov 2024 00:55:22 +0800 Subject: [PATCH] keep the day of the month when shifting the date range forward or backward if the selected date range is a full month --- src/lib/datetime.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lib/datetime.js b/src/lib/datetime.js index 88f43a40..cc36825a 100644 --- a/src/lib/datetime.js +++ b/src/lib/datetime.js @@ -355,6 +355,7 @@ export function getShiftedDateRange(minTime, maxTime, scale) { const firstDayOfMonth = minDateTime.clone().startOf('month'); const lastDayOfMonth = maxDateTime.clone().endOf('month'); + // check whether the date range matches full months if (firstDayOfMonth.unix() === minDateTime.unix() && lastDayOfMonth.unix() === maxDateTime.unix()) { const months = getYear(maxDateTime) * 12 + getMonth(maxDateTime) - getYear(minDateTime) * 12 - getMonth(minDateTime) + 1; const newMinDateTime = minDateTime.add(months * scale, 'months'); @@ -366,6 +367,18 @@ export function getShiftedDateRange(minTime, maxTime, scale) { }; } + // check whether the date range matches one full month + if (minDateTime.clone().add(1, 'months').subtract(1, 'seconds').unix() === maxDateTime.unix() || + maxDateTime.clone().subtract(1, 'months').add(1, 'seconds').unix() === minDateTime.unix()) { + const newMinDateTime = minDateTime.add(1 * scale, 'months'); + const newMaxDateTime = maxDateTime.add(1 * scale, 'months'); + + return { + minTime: newMinDateTime.unix(), + maxTime: newMaxDateTime.unix() + }; + } + const range = (maxTime - minTime + 1) * scale; return {