Skip to content

Commit

Permalink
fix: date math lol
Browse files Browse the repository at this point in the history
  • Loading branch information
kiosion committed Jan 25, 2024
1 parent fb1c634 commit b340791
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions svelte-app/src/lib/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '';
Expand Down

0 comments on commit b340791

Please sign in to comment.