0

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
  }

thammada.ts
  • 5,065
  • 2
  • 22
  • 33
Ion Utale
  • 551
  • 1
  • 9
  • 22
  • You have logical errors there, apart from non 30 day months, `60 * 60 * 24 * 7 * 30` are not the seconds on a month – Isidrok Jun 19 '20 at 13:48
  • Why don't you [customise relative time](https://day.js.org/docs/en/customization/relative-time) format and use [time from now](https://day.js.org/docs/en/display/from-now) ? – thammada.ts Jun 19 '20 at 14:20
  • thank you @Isidrok, that was a copy/paste typo – Ion Utale Jun 19 '20 at 15:10
  • @thammada.ts that is actually very nice. this is the first time i use dayjs, didn't know dayjs can do that – Ion Utale Jun 19 '20 at 15:12

1 Answers1

1

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)
Isidrok
  • 2,015
  • 10
  • 15