File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments