import dayjs from 'dayjs' ; export const formatSeconds = ( secs ) => { if ( secs < 60 ) return `${ secs } sec` ; const min = Math . floor ( secs / 60 ) ; const s = secs - min * 60 ; return `${ min } min${ s > 0 ? ` ${ s } sec` : '' }` ; } ; export const timeago = ( a , b ) => { const bigger = Math . max ( a , b ) ; const less = Math . min ( a , b ) ; let secs = ( bigger - less ) / 1000 ; const hour = Math . floor ( secs / 3600 ) ; secs = secs - hour * 3600 ; const minute = Math . floor ( secs / 60 ) ; secs = secs - minute * 60 ; const second = secs ; return { hour , minute , second , } ; } ; export function getTimeSpan ( times ) { if ( isNaN ( + times ) ) { times = 0 ; } const d = parseInt ( times / 60 / 60 / 24 + '' ) ; const h = parseInt ( ( ( times / 60 / 60 ) % 24 ) + '' ) ; const m = parseInt ( ( ( times / 60 ) % 60 ) + '' ) ; const s = parseInt ( ( times % 60 ) + '' ) ; return { d , h , m , s , } ; } export const formatTimeReadable = ( timeElapse ) => { let timeStr = '' ; const { d , h , m , s } = getTimeSpan ( timeElapse ) ; if ( d ) timeStr = `${ d } day` + ( d > 1 ? 's' : '' ) ; if ( h && ! timeStr ) timeStr = `${ h } hr` + ( h > 1 ? 's' : '' ) ; if ( m && ! timeStr ) timeStr = `${ m } min` + ( m > 1 ? 's' : '' ) ; if ( ! timeStr ) timeStr = `${ s } sec` + ( s > 1 ? 's' : '' ) ; return timeStr ; } ; export const getTimeFromNow = ( create_at ) => formatTimeReadable ( Date . now ( ) / 1000 - create_at ) ; export function fromNow ( time , currTime ? ) { let successTimeView = '' ; const successTime = getTimeSpan ( ( currTime || Date . now ( ) / 1000 ) - time ) ; const { d , h , m , s } = successTime ; let str = '' ; let flag = 0 ; if ( d ) { str += `${ d }${ `day${ d > 1 ? 's' : '' }` } ` ; flag ++ ; } if ( ( h || flag ) && flag < 3 ) { str += `${ h }${ `hr${ h > 1 ? 's' : '' }` } ` ; flag ++ ; } if ( ( m || flag ) && flag < 3 ) { str += `${ m }${ `min${ m > 1 ? 's' : '' }` } ` ; flag ++ ; } if ( ( s || flag ) && flag < 2 ) { str += `${ s }${ `sec${ s > 1 ? 's' : '' }` }` ; } if ( str ) successTimeView = str ; return successTimeView ; } export const sinceTime = ( time ) => { return Date . now ( ) / 1000 - time < 3600 * 24 ? `${ fromNow ( time ) } ago` : dayjs ( time * 1000 ) . format ( 'YYYY/MM/DD HH:mm' ) ; } ;