Skip to content

Commit 369bf2b

Browse files
committed
chore(landing-page): improve duration label
1 parent e7443c1 commit 369bf2b

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

landing/components/time-slice-example.tsx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import React from 'react'
22
import { TimeSlice } from '@lib'
33
import type { DateRange } from '@lib/timeslice'
44
import { ChevronDown } from 'lucide-react'
5+
import {
6+
differenceInYears,
7+
differenceInMonths,
8+
differenceInWeeks,
9+
differenceInDays,
10+
differenceInHours,
11+
differenceInMinutes,
12+
differenceInSeconds
13+
} from 'date-fns'
514

615
export default function TimeSliceExample() {
716
const [dateRange, setDateRange] = React.useState<DateRange>({
@@ -15,23 +24,22 @@ export default function TimeSliceExample() {
1524
}
1625

1726
const getDurationLabel = (start: Date, end: Date) => {
18-
const ms = end.getTime() - start.getTime()
27+
const diffSeconds = differenceInSeconds(end, start)
28+
const diffMinutes = differenceInMinutes(end, start)
29+
const diffHours = differenceInHours(end, start)
30+
const diffDays = differenceInDays(end, start)
31+
const diffWeeks = differenceInWeeks(end, start)
32+
const diffMonths = differenceInMonths(end, start)
33+
const diffYears = differenceInYears(end, start)
1934

20-
const minute = 60 * 1000
21-
const hour = 60 * minute
22-
const day = 24 * hour
23-
const week = 7 * day
24-
const month = 30.44 * day
25-
const year = 365.25 * day
35+
if (diffYears > 0) return `${diffYears}y`
36+
if (diffMonths > 0) return `${diffMonths}mo`
37+
if (diffWeeks > 0) return `${diffWeeks}w`
38+
if (diffDays > 0) return `${diffDays}d`
39+
if (diffHours > 0) return `${diffHours}h`
40+
if (diffMinutes > 0) return `${diffMinutes}m`
2641

27-
if (ms >= year) return `${Math.floor(ms / year)}y`
28-
if (ms >= month) return `${Math.floor(ms / month)}mo`
29-
if (ms >= week) return `${Math.floor(ms / week)}w`
30-
if (ms >= day) return `${Math.floor(ms / day)}d`
31-
if (ms >= hour) return `${Math.floor(ms / hour)}h`
32-
if (ms >= minute) return `${Math.floor(ms / minute)}m`
33-
34-
return `${Math.floor(ms / 1000)}s`
42+
return `${diffSeconds}s`
3543
}
3644

3745
const activeDurationLabel = React.useMemo(() => {

0 commit comments

Comments
 (0)