よくあるやつ 日までのみ対応

/**
 * 日時1から日時2がどれだけ過去かを文字列で表現して返す
 * 10秒前までなら "今"、10秒前なら "10秒前"、61秒前なら "1分前" みたいに
 * @param {Date} dt1 未来
 * @param {Date} dt2 過去
 * @return {String} 秒>分>時間>日 のうち最小単位
 */
const getOffsetString = (dt1, dt2) => {
  const offsetTs = dt1.getTime() - dt2.getTime();

  if (offsetTs < 10000) {
    return '今';
  } else if (offsetTs < 60000) {
    return `${Math.floor(offsetTs / 1000)}秒前`;
  } else if (offsetTs < 3600000) {
    return `${Math.floor(offsetTs / 60000)}分前`;
  } else if (offsetTs < 86400000) {
    return `${Math.floor(offsetTs / 3600000)}時間前`;
  } else {
    return `${Math.floor(offsetTs / 86400000)}日前`;
  }
};

以下通ったテスト

const now = new Date(2000, 11, 31, 9, 0, 0);
console.assert(getOffsetString(now, new Date(2000, 11, 31, 9,  0,  0)) === '今');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8, 59, 51)) === '今');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8, 59, 50)) === '10秒前');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8, 59,  1)) === '59秒前');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8, 59,  0)) === '1分前');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8, 58, 59)) === '1分前');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8,  0,  1)) === '59分前');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 8,  0,  0)) === '1時間前');
console.assert(getOffsetString(now, new Date(2000, 11, 31, 7, 59, 59)) === '1時間前');
console.assert(getOffsetString(now, new Date(2000, 11, 30, 9,  0,  1)) === '23時間前');
console.assert(getOffsetString(now, new Date(2000, 11, 30, 9,  0,  0)) === '1日前');
console.assert(getOffsetString(now, new Date(2000, 11, 30, 8, 59, 59)) === '1日前');
console.assert(getOffsetString(now, new Date(2000, 11, 29, 9,  0,  0)) === '2日前');