From b3407918ee391536c53417fa0f7f337a202f5878 Mon Sep 17 00:00:00 2001 From: kiosion Date: Wed, 24 Jan 2024 20:58:22 -0500 Subject: [PATCH] fix: date math lol --- svelte-app/src/lib/date.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/svelte-app/src/lib/date.ts b/svelte-app/src/lib/date.ts index 0c333e8f0..9e2777eea 100644 --- a/svelte-app/src/lib/date.ts +++ b/svelte-app/src/lib/date.ts @@ -39,20 +39,21 @@ export const displayMonthDuration = derived< } const start = new Date(startDate), - end = endDate ? new Date(endDate) : new Date(); + end = endDate ? new Date(endDate) : new Date(), + diffMs = end.getTime() - start.getTime(), + diffDays = Math.floor(diffMs / 86400000), + diffMonths = Math.floor(diffDays / 30); - let years = end.getFullYear() - start.getFullYear(); - let months = end.getMonth() - start.getMonth(); + if (diffMonths === 0 || diffMonths === 1) { + return t('{month} month', { month: 1 }); + } - // Adjust for cases where the end month is earlier in the year than the start month - if (months < 0) { - years--; - months += 12; // Add the months that have passed in the current year + if (diffMonths < 12) { + return t('{month} months', { month: diffMonths }); } - // Consolidate total duration into years and months - years += Math.floor(months / 12); - months %= 12; + const years = Math.floor(diffMonths / 12), + months = diffMonths % 12; let yearString = '', monthString = '';