Source

dateTime/getRelativeDateTime.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isDateString_1 = __importDefault(require("./isDateString"));
var isStr_1 = __importDefault(require("../string/isStr"));
var ONE_SECOND_MS = 1000;
var MAX_SECONDS = 60;
var MAX_MINS = 60;
var MAX_HOURS = 24;
var MAX_DAYS_IN_WEEK = 7;
var MAX_DAYS_IN_MONTH = 30;
var MAX_DAYS_IN_YEAR = 365;
var SECOND_PRECISION = 10;
/**
 * Get relative date time string e.g. 3 days ago, 3 weeks ago
 *
 * * This function require "Intl.RelativeTimeFormat" in global scope.
 * * Please see polyfill/polyfillIntl if you need a polyfill of that.
 *
 * @param comparedDateTime ISO-timestamp or Date instance
 * @param relatedDateTime ISO-timestamp or Date instance
 * @param locales The locale code or an array of locale codes
 * @category dateTime
 * @module getRelativeDateTime
 */
function getRelativeDateTime(comparedDateTime, relatedDateTime, locales) {
    var comparedDate = (0, isStr_1.default)(comparedDateTime)
        ? new Date((0, isDateString_1.default)(comparedDateTime) ? comparedDateTime : 0)
        : comparedDateTime || new Date();
    var relativeDate = (0, isStr_1.default)(relatedDateTime)
        ? new Date((0, isDateString_1.default)(relatedDateTime) ? relatedDateTime : 0)
        : relatedDateTime || new Date();
    // @ts-expect-error: Polyfill is required
    var rtf = new Intl.RelativeTimeFormat(locales, { numeric: 'auto' });
    var diffInSecond = ((comparedDate.getTime() - relativeDate.getTime()) / ONE_SECOND_MS);
    var diffInMins = diffInSecond / MAX_SECONDS;
    if (Math.abs(Math.round(diffInSecond)) === 0) {
        return rtf.format(0, 'second');
    }
    else if (Math.abs(diffInSecond) < MAX_SECONDS) {
        // Minimum precision to every ten second
        var numSecs = Math.round(diffInSecond / SECOND_PRECISION) * SECOND_PRECISION;
        return rtf.format(numSecs, 'second');
    }
    else if (Math.abs(diffInMins) < MAX_MINS) {
        return rtf.format(Math.round(diffInMins), 'minute');
    }
    var diffInHour = diffInMins / MAX_MINS;
    if (Math.abs(diffInHour) < MAX_HOURS)
        return rtf.format(Math.round(diffInHour), 'hour');
    var diffInDay = diffInHour / MAX_HOURS;
    if (Math.round(diffInDay) % MAX_DAYS_IN_YEAR === 0) {
        var diffInYear = diffInDay / MAX_DAYS_IN_YEAR;
        return rtf.format(Math.round(diffInYear), 'year');
    }
    else if (Math.round(diffInDay) % MAX_DAYS_IN_MONTH === 0) {
        var diffInMonth = diffInDay / MAX_DAYS_IN_MONTH;
        return rtf.format(Math.round(diffInMonth), 'month');
    }
    else if (Math.round(diffInDay) % MAX_DAYS_IN_WEEK === 0) {
        var diffInWeek = diffInDay / MAX_DAYS_IN_WEEK;
        return rtf.format(Math.round(diffInWeek), 'week');
    }
    return rtf.format(Math.round(diffInDay), 'day');
}
exports.default = getRelativeDateTime;