Search code examples
javascriptecmascript-6timedayjs

how can i improve method that returns created time diff like ( 30s, 5m, 3h, 1y ago ) in >= ES6


here is my code

what i am trying to achieve is the time similar to youtube/fb/instagram, show the posting time diff

like: 30s ago 5m ago 15h ago 6d ago 1w ago 5y ago

the code is working fine, but is to verbose, and i am wondering if some js "guru" can improve this using latest features of ecmascript

 const timeAgo = () => {
    const date1 = dayjs(Date.now())
    const now = base.createdOn
    const diffInSeconds = date1.diff(now, 'second', false)

    let diff = date1.diff(now, 'second', false) + 's'

    if (diffInSeconds > 60 * 60 * 24 * 7 * 30 * 12) {
      return date1.diff(now, 'year', false) + 'y'
    }

    if (diffInSeconds > 60 * 60 * 24 * 7 * 30) {
      return date1.diff(now, 'month', false) + 'm'
    }

    if (diffInSeconds > 60 * 60 * 24 * 7) {
      return date1.diff(now, 'week', false) + 'w'
    }

    if (diffInSeconds > 60 * 60 * 24) {
      return date1.diff(now, 'day', false) + 'd'
    }

    if (diffInSeconds > 60 * 60) {
      return date1.diff(now, 'hour', false) + 'h'
    }

    if (diffInSeconds > 60) {
      return date1.diff(now, 'minute', false) + 'm'
    }

    return diff
  }


Solution

  • A for of should do it and if you let day.js compare you won't have logical errors:

     const timeSince = (from, to = Date.now()) => {
        to = dayjs(to);
        const units = ['year', 'month', 'week', 'day', 'hour', 'minute'];
        for (let unit of units){
            const diff = to.diff(from, unit, false);
            if(diff) {
                return diff + unit.charaAt(0); // you are using m for month and minute...
            }
        }
        // base case if there are no seconds difference
        return to.diff(now, 'second', false) + 's';
    
      }
    
    // timeSince(base.createdOn)