0

我使用dayJS并希望仅显示一天的相对时间 - 24 小时

有没有办法自定义 fromNow() 只显示一天?

例子

currentDate = 'Sun Aug 15 2021 15:03:09 GMT+0300';

oldDate = 'Fri Aug 13 2021 15:03:09 GMT+0300';

created_at = dayjs(currentDate).fromNow(true) => 17 min 2 sec // ok ✅ 

// if +=1 day
created_at = 
dayjs(oldDate).fromNow(true) => 2 day // hidden this one
4

1 回答 1

0

这是我尝试过的解决方法:)如果还有其他解决方案,请告诉我:)

 let acceptedDate = ['few seconds', 'minute', 'minutes', 'hour', 'hours'];
  let currentDate = dayjs();
  let targetedDay = created_at;// from api
  
  let currentRelativeTime = dayjs(targetedDay).from(currentDate, true);
  let timeArr = currentRelativeTime.split(' ');
  let isAcceptedDate = acceptedDate.some(item => timeArr.includes(item));
  console.log(isAcceptedDate);
于 2021-08-15T12:52:56.603 回答