Skip to content

Commit 2d8b129

Browse files
committed
chore(landing-page): improve getDurationLabel
1 parent 851c1e1 commit 2d8b129

2 files changed

Lines changed: 26 additions & 40 deletions

File tree

landing/components/code-examples.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,21 @@ export function CustomTimeRangePicker() {
4343
const getDurationLabel = (start, end) => {
4444
const ms = end.getTime() - start.getTime()
4545
46-
// Calculate months difference
47-
const monthDiff = (end.getMonth() + end.getFullYear() * 12) -
48-
(start.getMonth() + start.getFullYear() * 12)
46+
const minute = 60 * 1000
47+
const hour = 60 * minute
48+
const day = 24 * hour
49+
const week = 7 * day
50+
const month = 30.44 * day
51+
const year = 365.25 * day
4952
50-
if (monthDiff >= 12) return \`\${Math.floor(monthDiff/12)}y\`
51-
if (monthDiff > 0) return \`\${monthDiff}mo\`
53+
if (ms >= year) return \`\${Math.floor(ms / year)}y\`
54+
if (ms >= month) return \`\${Math.floor(ms / month)}mo\`
55+
if (ms >= week) return \`\${Math.floor(ms / week)}w\`
56+
if (ms >= day) return \`\${Math.floor(ms / day)}d\`
57+
if (ms >= hour) return \`\${Math.floor(ms / hour)}h\`
58+
if (ms >= minute) return \`\${Math.floor(ms / minute)}m\`
5259
53-
const days = Math.floor(ms / (1000 * 60 * 60 * 24))
54-
const weeks = Math.floor(days / 7)
55-
if (weeks > 0) return \`\${weeks}w\`
56-
if (days > 0) return \`\${days}d\`
57-
58-
const hours = Math.floor(ms / (1000 * 60 * 60))
59-
if (hours > 0) return \`\${hours}h\`
60-
61-
const minutes = Math.floor(ms / (1000 * 60))
62-
if (minutes > 0) return \`\${minutes}m\`
63-
64-
const seconds = Math.floor(ms / 1000)
65-
return \`\${seconds}s\`
60+
return \`\${Math.floor(ms / 1000)}s\`
6661
}
6762
6863
// Calculate the active duration label

landing/components/time-slice-example.tsx

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

20-
const yearDiff = end.getFullYear() - start.getFullYear()
21-
const monthDiff = end.getMonth() - start.getMonth() + yearDiff * 12
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
2226

23-
const isFullMonth =
24-
start.getDate() >= end.getDate() ||
25-
(end.getDate() >= start.getDate() && monthDiff > 1)
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`
2633

27-
if (monthDiff >= 12) return `${Math.floor(monthDiff / 12)}y`
28-
if (monthDiff > 0 && isFullMonth) return `${monthDiff}mo`
29-
30-
const days = Math.floor(ms / (1000 * 60 * 60 * 24))
31-
const weeks = Math.floor(days / 7)
32-
33-
if (weeks > 0) return `${weeks}w`
34-
if (days > 0) return `${days}d`
35-
36-
const hours = Math.floor(ms / (1000 * 60 * 60))
37-
if (hours > 0) return `${hours}h`
38-
39-
const minutes = Math.floor(ms / (1000 * 60))
40-
if (minutes > 0) return `${minutes}m`
41-
42-
const seconds = Math.floor(ms / 1000)
43-
return `${seconds}s`
34+
return `${Math.floor(ms / 1000)}s`
4435
}
4536

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

0 commit comments

Comments
 (0)