import moment from 'moment-timezone' ; param momentTime function formatTimezone ( momentTime ) { const DEFAULT_TIMEZONE_FORMAT = 'Z' ; const utcOffsetHours = momentTime . utcOffset ( ) / 60 ; const customTimezoneFormat = utcOffsetHours > 0 ? `+${ utcOffsetHours }` : utcOffsetHours ; const utcOffsetFormatted = Number . isInteger ( utcOffsetHours ) ? customTimezoneFormat : DEFAULT_TIMEZONE_FORMAT ; return momentTime . format ( `(UTC${ utcOffsetFormatted })` ) ; } export type TimeUnit = 'hours' | 'minutes' | 'seconds' | 'milliseconds' ; function getTimeFormat ( timeUnit ) { switch ( timeUnit ) { case 'hours' : return 'HH' ; case 'minutes' : return 'HH:mm' ; case 'seconds' : return 'HH:mm:ss' ; case 'milliseconds' : return 'HH:mm:ss.SSS' ; default : return '' ; } } type DateUnit = 'days' | 'months' | 'years' ; function getDateFormat ( dateUnit ) { switch ( dateUnit ) { case 'years' : return 'YYYY' ; case 'months' : return 'MMM YYYY' ; case 'days' : return 'MMM D, YYYY' ; default : return '' ; } } export const getDateDifference = ( { start , end , unitOfTime , precise , } ) => end . diff ( start , unitOfTime , precise ) ; function getFormatsAccordingToDateDifference ( start , end ) { if ( getDateDifference ( { start , end , unitOfTime : 'years' } ) >= 5 ) { return { dateFormat : getDateFormat ( 'years' ) } ; } if ( getDateDifference ( { start , end , unitOfTime : 'months' } ) >= 5 ) { return { dateFormat : getDateFormat ( 'months' ) } ; } const dateFormatWithDays = getDateFormat ( 'days' ) ; if ( getDateDifference ( { start , end , unitOfTime : 'days' } ) > 1 ) { return { dateFormat : dateFormatWithDays } ; } if ( getDateDifference ( { start , end , unitOfTime : 'minutes' } ) >= 1 ) { return { dateFormat : dateFormatWithDays , timeFormat : getTimeFormat ( 'minutes' ) , } ; } if ( getDateDifference ( { start , end , unitOfTime : 'seconds' } ) >= 10 ) { return { dateFormat : dateFormatWithDays , timeFormat : getTimeFormat ( 'seconds' ) , } ; } return { dateFormat : dateFormatWithDays , timeFormat : getTimeFormat ( 'milliseconds' ) , } ; } export function asAbsoluteDateTime ( time , timeUnit = 'milliseconds' ) { const momentTime = moment ( time ) ; const formattedTz = formatTimezone ( momentTime ) ; return momentTime . format ( `${ getDateFormat ( 'days' ) }, ${ getTimeFormat ( timeUnit ) } ${ formattedTz }` ) ; } param start param end export function asRelativeDateTimeRange ( start , end ) { const momentStartTime = moment ( start ) ; const momentEndTime = moment ( end ) ; const { dateFormat , timeFormat } = getFormatsAccordingToDateDifference ( momentStartTime , momentEndTime ) ; if ( timeFormat ) { const startFormatted = momentStartTime . format ( `${ dateFormat }, ${ timeFormat }` ) ; const endFormatted = momentEndTime . format ( timeFormat ) ; const formattedTz = formatTimezone ( momentStartTime ) ; return `${ startFormatted } - ${ endFormatted } ${ formattedTz }` ; } const startFormatted = momentStartTime . format ( dateFormat ) ; const endFormatted = momentEndTime . format ( dateFormat ) ; return `${ startFormatted } - ${ endFormatted }` ; }