1

Cooldown Time Humanizer

Function JavaScript 2 revisions 276 7 months ago 7 months ago
// Time should be milliseconds left

function parseTime (time, friendly, shortened) {
    let fm = [
      Math.floor(time / 60 / 60 / 24 / 30), // MONTHS
      Math.floor(time / 60 / 60 / 24) % 30, // DAYS
      Math.floor(time / 60 / 60) % 24, // HOURS
      Math.floor(time / 60) % 60, // MINUTES
      Math.floor(time % 60) // SECONDS
    ];
    if (friendly) {
      const timeArr = [ { type: { singular: 'month', plural: 'months' }, amount: fm[0] },
        { type: { singular: 'day', plural: 'days' }, amount: fm[1] },
        { type: { singular: 'hour', plural: 'hours' }, amount: fm[2] },
        { type: { singular: 'minute', plural: 'minutes' }, amount: fm[3] },
        { type: { singular: 'second', plural: 'seconds' }, amount: fm[4] }
      ];
      let properArr = [];
      for (let i in timeArr) {
        if (timeArr[i].amount < 1) continue;
        properArr.push(`${timeArr[i].amount}${shortened ? '' : ' '}${(timeArr[i].amount === 1 ? timeArr[i].type.singular : timeArr[i].type.plural).slice(0, shortened ? 1 : undefined)}`);
      }
      return (properArr.slice(0, -2).join(', ') + (properArr.slice(0, -2).length ? ', ' : '') + properArr.slice(-2).join(' and ')) || (shortened ? '0s' : '0 seconds');
    }

    let checked = false;
    return fm.filter(i => { if (checked || i) { checked = true; return checked; } }).map(v => { return ((v < 10) ? '0' : '') + v; }).join(':');
  }

Used to parse time for our bot command cooldowns (https://i.imgur.com/o76f0MA.png) on Dank Memer (https://dankmemer.lol/) Feedback appreciated