@@ -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