Skip to content

Commit

Permalink
keep the day of the month when shifting the date range forward or bac…
Browse files Browse the repository at this point in the history
…kward if the selected date range is a full month
  • Loading branch information
mayswind committed Nov 4, 2024
1 parent 1c90611 commit c3a880e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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 {
Expand Down

0 comments on commit c3a880e

Please sign in to comment.