From 1184ba9ecd2cbd15bc81ca844018d13667e7693c Mon Sep 17 00:00:00 2001 From: Spencer Spenst Date: Tue, 19 Mar 2024 12:48:31 -0700 Subject: [PATCH] fix dayjs rounding --- components/formatted/formattedDate.tsx | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/components/formatted/formattedDate.tsx b/components/formatted/formattedDate.tsx index 5ce157cd7..f2201bd2d 100644 --- a/components/formatted/formattedDate.tsx +++ b/components/formatted/formattedDate.tsx @@ -4,7 +4,29 @@ import relativeTime from 'dayjs/plugin/relativeTime'; import React from 'react'; import StyledTooltip from '../page/styledTooltip'; -dayjs.extend(relativeTime); +// https://day.js.org/docs/en/customization/relative-time +dayjs.extend( + relativeTime, + { + rounding: Math.floor, + thresholds: [ + { l: 's', r: 59, d: 'second' }, + { l: 'm', r: 1 }, + { l: 'mm', r: 59, d: 'minute' }, + { l: 'h', r: 1 }, + { l: 'hh', r: 23, d: 'hour' }, + { l: 'd', r: 1 }, + { l: 'dd', r: 27, d: 'day' }, + // NB: weeks are not well-supported by dayjs + // { l: 'w', r: 1 }, + // { l: 'ww', r: 4, d: 'week' }, + { l: 'M', r: 1 }, + { l: 'MM', r: 11, d: 'month' }, + { l: 'y', r: 1 }, + { l: 'yy', d: 'year' }, + ], + } +); interface FormattedDateProps { className?: string;