Skip to content

Commit 851c1e1

Browse files
committed
chore(landing-page): fix getDurationLabel
1 parent fead8aa commit 851c1e1

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

landing/components/time-slice-example.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@ export default function TimeSliceExample() {
1717
const getDurationLabel = (start: Date, end: Date) => {
1818
const ms = end.getTime() - start.getTime()
1919

20-
const monthDiff =
21-
end.getMonth() +
22-
end.getFullYear() * 12 -
23-
(start.getMonth() + start.getFullYear() * 12)
20+
const yearDiff = end.getFullYear() - start.getFullYear()
21+
const monthDiff = end.getMonth() - start.getMonth() + yearDiff * 12
22+
23+
const isFullMonth =
24+
start.getDate() >= end.getDate() ||
25+
(end.getDate() >= start.getDate() && monthDiff > 1)
2426

2527
if (monthDiff >= 12) return `${Math.floor(monthDiff / 12)}y`
26-
if (monthDiff > 0) return `${monthDiff}mo`
28+
if (monthDiff > 0 && isFullMonth) return `${monthDiff}mo`
2729

2830
const days = Math.floor(ms / (1000 * 60 * 60 * 24))
2931
const weeks = Math.floor(days / 7)
32+
3033
if (weeks > 0) return `${weeks}w`
3134
if (days > 0) return `${days}d`
3235

0 commit comments

Comments
 (0)