mirror of
https://github.com/cheeaun/phanpy.git
synced 2024-11-22 01:05:34 +03:00
Fix relative time ago exceeding 24 hours
This commit is contained in:
parent
ac94303d0c
commit
377ad11c1a
1 changed files with 10 additions and 10 deletions
|
@ -66,31 +66,31 @@ export default function RelativeTime({ datetime, format }) {
|
|||
const date = useMemo(() => dayjs(datetime), [datetime]);
|
||||
const [dateStr, dt, title] = useMemo(() => {
|
||||
if (!date.isValid()) return ['' + datetime, '', ''];
|
||||
const realDate = date.toDate();
|
||||
let str;
|
||||
if (format === 'micro') {
|
||||
// If date <= 1 day ago or day is within this year
|
||||
const now = dayjs();
|
||||
const dayDiff = now.diff(date, 'day');
|
||||
const now = new Date();
|
||||
const dayDiff = (now.getTime() - realDate.getTime()) / 1000 / day;
|
||||
if (dayDiff <= 1) {
|
||||
str = twitterFromNow(date.toDate());
|
||||
str = twitterFromNow(realDate);
|
||||
} else {
|
||||
const currentYear = now.year();
|
||||
const dateYear = date.year();
|
||||
if (dateYear === currentYear) {
|
||||
const sameYear = now.getFullYear() === realDate.getFullYear();
|
||||
if (sameYear) {
|
||||
str = DTF(i18n.locale, {
|
||||
year: undefined,
|
||||
month: 'short',
|
||||
day: 'numeric',
|
||||
}).format(date.toDate());
|
||||
}).format(realDate);
|
||||
} else {
|
||||
str = DTF(i18n.locale, {
|
||||
dateStyle: 'short',
|
||||
}).format(date.toDate());
|
||||
}).format(realDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!str) str = rtfFromNow(date.toDate());
|
||||
return [str, date.toISOString(), date.toDate().toLocaleString()];
|
||||
if (!str) str = rtfFromNow(realDate);
|
||||
return [str, realDate.toISOString(), realDate.toLocaleString()];
|
||||
}, [date, format, renderCount]);
|
||||
|
||||
useEffect(() => {
|
||||
|
|
Loading…
Reference in a new issue