diff --git a/src/backend/model/database/SearchManager.ts b/src/backend/model/database/SearchManager.ts index 75010fdb..9977ca65 100644 --- a/src/backend/model/database/SearchManager.ts +++ b/src/backend/model/database/SearchManager.ts @@ -779,7 +779,7 @@ export class SearchManager { break; case DatePatternFrequency.months_ago: - to.setUTCMonth(to.getUTCMonth() - tq.agoNumber); + to.setTime(Utils.addMonthToDate(to, -1 * tq.agoNumber).getTime()); break; case DatePatternFrequency.years_ago: diff --git a/src/common/Utils.ts b/src/common/Utils.ts index a9a7a862..a47898ce 100644 --- a/src/common/Utils.ts +++ b/src/common/Utils.ts @@ -220,6 +220,19 @@ export class Utils { return dayOfYear; } + //Adding months to a date differently from standard JS + //this function makes sure that if date is the 31st and you add a month, you will get the last day of the next month + //so adding or subtracting a month from 31st of march will give 30th of april or 28th of february respectively (29th on leap years). + static addMonthToDate(date: Date, numMonths: number) { + let result = new Date(date) + const expectedMonth = ((date.getMonth() + numMonths) % 12 + 12) % 12; //inner %12 + 12 makes correct handling of negative months + result.setMonth(result.getMonth() + numMonths); + if (result.getMonth() !== expectedMonth) { + result.setDate(0); + } + return result; + } + static renderDataSize(size: number): string { const postFixes = ['B', 'KB', 'MB', 'GB', 'TB']; let index = 0; diff --git a/test/tmp/sqlite.db-journal b/test/tmp/sqlite.db-journal deleted file mode 100644 index 41fef9ee..00000000 Binary files a/test/tmp/sqlite.db-journal and /dev/null differ