Skip to content

Instantly share code, notes, and snippets.

@brandonmcconnell
Last active July 26, 2022 04:11
Show Gist options
  • Select an option

  • Save brandonmcconnell/4a177fd6af7cffd4ca4808b3298b930c to your computer and use it in GitHub Desktop.

Select an option

Save brandonmcconnell/4a177fd6af7cffd4ca4808b3298b930c to your computer and use it in GitHub Desktop.

Revisions

  1. brandonmcconnell revised this gist Jun 28, 2022. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions getTimeSince.js
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,11 @@ const ordinalizeNumber = n => {
    }

    const getTimeSince = (_fromDate, _toDate, dateStringCap) => {
    const toPresent = _toDate === undefined;
    const throwError = () => {
    throw new Error('getTimeSince requires 1-2 arguments, of type date or date-string');
    };
    if (typeof _fromDate === 'undefined') throwError();
    if (typeof _fromDate === 'undefined' || isNaN(Number(_fromDate)) || (typeof _toDate !== 'undefined' && isNaN(Number(_toDate)))) throwError();
    const toPresent = _toDate === undefined || Math.abs(Number(_toDate) - Number(new Date())) < 50;
    const fromDate = Number(new Date(_fromDate));
    const toDate = Number(_toDate === undefined ? new Date() : new Date(_toDate ?? null));
    if (isNaN(fromDate) || isNaN(toDate)) throwError();
  2. brandonmcconnell created this gist Jun 28, 2022.
    58 changes: 58 additions & 0 deletions getTimeSince.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    const ordinalizeNumber = n => {
    const rule = new Intl.PluralRules('en-US', { type: 'ordinal' }).select(n);
    const suffix = ({
    one: 'st',
    two: 'nd',
    few: 'rd',
    other: 'th',
    })[rule];
    return `${n}${suffix}`;
    }

    const getTimeSince = (_fromDate, _toDate, dateStringCap) => {
    const toPresent = _toDate === undefined;
    const throwError = () => {
    throw new Error('getTimeSince requires 1-2 arguments, of type date or date-string');
    };
    if (typeof _fromDate === 'undefined') throwError();
    const fromDate = Number(new Date(_fromDate));
    const toDate = Number(_toDate === undefined ? new Date() : new Date(_toDate ?? null));
    if (isNaN(fromDate) || isNaN(toDate)) throwError();
    let formatTimeSince = new Intl.RelativeTimeFormat('en', {
    localeMatcher: 'best fit',
    style: 'long',
    numeric: 'auto',
    });
    let diff = fromDate - toDate;
    if (!isNaN(dateStringCap) && Math.abs(diff) >= Number(dateStringCap)) {
    const [weekday, month, dateNumStr, year] = (
    new Date(fromDate).toLocaleString('en-us', {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    year: 'numeric',
    }).replaceAll(',', '').split(' ')
    );
    return `${weekday} ${month} ${ordinalizeNumber(Number(dateNumStr))}, ${year}`;
    }
    if (Math.abs(diff) < 1000) return toPresent ? 'now' : 'simultaneously';
    for (const [unit, value] of [
    ['year', 1000 * 60 * 60 * 24 * 365],
    ['month', 1000 * 60 * 60 * 24 * 31],
    ['week', 1000 * 60 * 60 * 24 * 7],
    ['day', 1000 * 60 * 60 * 24],
    ['hour', 1000 * 60 * 60],
    ['minute', 1000 * 60],
    ['second', 1000],
    ]) if (Math.abs(diff) >= value) {
    const { sign, floor, ceil } = Math;
    let result = formatTimeSince.format(
    (sign(diff) === 1 ? floor : ceil)(diff / value),
    unit
    );
    if (!toPresent) result = Math.sign(diff) === 1
    ? result.replace('in ', '') + ' later'
    : result.replace('ago', 'prior');
    return result;
    }
    }