Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 1x 9x 8x 8x 8x 8x 8x 8x 8x 1x 7x 3x 4x 2x 2x | import { IntlShape } from 'react-intl';
import { Time } from '@/enums/date';
export const formatRelativeTime = (timestamp: number | undefined, intl: IntlShape): string => {
if (!timestamp) return '';
const date = new Date(timestamp * Time.ONE_SECOND);
const now = new Date();
const diffInMilliseconds = now.getTime() - date.getTime();
const diffInMinutes = Math.floor(diffInMilliseconds / Time.ONE_MINUTE);
const diffInHours = Math.floor(diffInMilliseconds / Time.ONE_HOUR);
const diffInDays = Math.floor(diffInMilliseconds / Time.ONE_DAY);
if (diffInMinutes < 1) {
return intl.formatMessage({ id: 'time.justNow' });
}
if (diffInMinutes < 60) {
return intl.formatMessage({ id: 'time.minutesAgo' }, { minutes: diffInMinutes });
}
if (diffInHours < 24) {
return intl.formatMessage({ id: 'time.hoursAgo' }, { hours: diffInHours });
}
return intl.formatMessage({ id: 'time.daysAgo' }, { days: diffInDays });
};
|