export class TimeDiff { isNegative : boolean ; seconds : number ; minutes : number ; hours : number ; days : number ; months : number ; years : number ; constructor ( years , months , days , hours , minutes , seconds , isNegative = false ) { this . years = years ; this . months = months ; this . days = days ; this . hours = hours ; this . minutes = minutes ; this . seconds = seconds ; this . isNegative = isNegative ; } } export function getTimeDiff ( date1 , date2 ) { let millisecondsDiff = ( date1 . getTime ( ) - date2 . getTime ( ) ) ; let isNegative = false ; if ( millisecondsDiff < 0 ) { isNegative = true ; millisecondsDiff *= - 1 ; } const years = Math . floor ( millisecondsDiff / 1000 / 60 / 60 / 24 / 30 / 12 ) ; millisecondsDiff -= years * 1000 * 60 * 60 * 24 * 30 * 12 ; const months = Math . floor ( millisecondsDiff / 1000 / 60 / 60 / 24 / 30 ) ; millisecondsDiff -= months * 1000 * 60 * 60 * 24 * 30 ; const days = Math . floor ( millisecondsDiff / 1000 / 60 / 60 / 24 ) ; millisecondsDiff -= days * 1000 * 60 * 60 * 24 ; const hours = Math . floor ( millisecondsDiff / 1000 / 60 / 60 ) ; millisecondsDiff -= hours * 1000 * 60 * 60 ; const minutes = Math . floor ( millisecondsDiff / 1000 / 60 ) ; millisecondsDiff -= minutes * 1000 * 60 ; const seconds = Math . floor ( millisecondsDiff / 1000 ) ; return new TimeDiff ( years , months , days , hours , minutes , seconds , isNegative ) ; } export function getTimeDiffFormat ( timeDiff ) { let result = `${ timeDiff . isNegative ? '-' : '' } ` ; if ( timeDiff . years > 0 ) { result += `${ timeDiff . years } yr: ${ timeDiff . months } mo: ${ timeDiff . days } days` ; } else if ( timeDiff . months > 0 ) { result += `${ timeDiff . months } mo: ${ timeDiff . days } days: ${ timeDiff . hours } hr` ; } else if ( timeDiff . days > 0 ) { result += `${ timeDiff . days } days: ${ timeDiff . hours } hr: ${ timeDiff . minutes } min` ; } else { result += `${ timeDiff . hours } hr: ${ timeDiff . minutes } min: ${ timeDiff . seconds } sec` ; } return result ; } export function getTimeFormat ( dateTime ) { return dateTime . toLocaleString ( 'en-GB' , { hour12 : false } ) ; }