import differenceInCalendarDays from '../differenceInCalendarDays/index' ; import format from '../format/index' ; import defaultLocale from '../locale/en-US/index' ; import subMilliseconds from '../subMilliseconds/index' ; import toDate from '../toDate/index' ; import getTimezoneOffsetInMilliseconds from '../_lib/getTimezoneOffsetInMilliseconds/index' ; import requiredArgs from '../_lib/requiredArgs/index' ; import type { LocaleOptions , WeekStartOptions } from '../types' ; import type { FormatRelativeToken } from '../locale/types' ; name category summary description param { Date | Number } date param { Date | Number } baseDate param options param { Locale } options . locale - the locale object. See [Locale] https param { 0 | 1 | 2 | 3 | 4 | 5 | 6 } options . weekStartsOn returns { String } throws throws throws throws throws throws throws example export default function formatRelative ( dirtyDate , dirtyBaseDate , dirtyOptions ? ) { requiredArgs ( 2 , arguments ) ; const date = toDate ( dirtyDate ) ; const baseDate = toDate ( dirtyBaseDate ) ; const { locale = defaultLocale , weekStartsOn = 0 } = dirtyOptions || { } ; if ( ! locale . localize ) { throw new RangeError ( 'locale must contain localize property' ) ; } if ( ! locale . formatLong ) { throw new RangeError ( 'locale must contain formatLong property' ) ; } if ( ! locale . formatRelative ) { throw new RangeError ( 'locale must contain formatRelative property' ) ; } const diff = differenceInCalendarDays ( date , baseDate ) ; if ( isNaN ( diff ) ) { throw new RangeError ( 'Invalid time value' ) ; } let token ; if ( diff < - 6 ) { token = 'other' ; } else if ( diff < - 1 ) { token = 'lastWeek' ; } else if ( diff < 0 ) { token = 'yesterday' ; } else if ( diff < 1 ) { token = 'today' ; } else if ( diff < 2 ) { token = 'tomorrow' ; } else if ( diff < 7 ) { token = 'nextWeek' ; } else { token = 'other' ; } const utcDate = subMilliseconds ( date , getTimezoneOffsetInMilliseconds ( date ) ) ; const utcBaseDate = subMilliseconds ( baseDate , getTimezoneOffsetInMilliseconds ( baseDate ) ) ; const formatStr = locale . formatRelative ( token , utcDate , utcBaseDate , { locale , weekStartsOn , } ) ; return format ( date , formatStr , { locale , weekStartsOn } ) ; }