@technobuddha/library
Version:
A large library of useful functions
76 lines (75 loc) • 3.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.relativeTime = void 0;
var constants_1 = require("../constants");
var floor_1 = __importDefault(require("../floor"));
var isSameDay_1 = __importDefault(require("../isSameDay"));
var formatDate_1 = __importDefault(require("../formatDate"));
var addTime_1 = __importDefault(require("../addTime"));
var plural_1 = __importDefault(require("../plural"));
/**
* Describe the difference between two dates in a simple format
*
* @param input The date
* @param relativeTo The date to compare to
* @param __namedParameters see {@link Options}
* @returns string describing the time difference between the two dates
*/
function relativeTime(input, relativeTo, _a) {
var _b = _a === void 0 ? {} : _a, _c = _b.todayTomorrowYesterday, todayTomorrowYesterday = _c === void 0 ? false : _c, _d = _b.timeFormat, timeFormat = _d === void 0 ? 'H:mm TT' : _d, _e = _b.ymdFormat, ymdFormat = _e === void 0 ? 'MMMM D YYYY' : _e, _f = _b.mdFormat, mdFormat = _f === void 0 ? 'MMMM D' : _f;
var text = [];
if (todayTomorrowYesterday) {
if (isSameDay_1.default(input, relativeTo))
text.push("today " + formatDate_1.default(input, timeFormat) + " -");
else if (isSameDay_1.default(input, addTime_1.default(relativeTo, { days: 1 })))
text.push("tomorrow " + formatDate_1.default(input, timeFormat) + " -");
else if (isSameDay_1.default(input, addTime_1.default(relativeTo, { days: -1 })))
text.push("yesterday " + formatDate_1.default(input, timeFormat) + " -");
}
var diff = (input.getTime() - relativeTo.getTime()) / constants_1.ticksPerSecond;
var sign = 1;
if (diff < 0) {
sign = -1;
diff = Math.abs(diff);
}
var d = floor_1.default((diff) / constants_1.secondsPerDay, { tolerance: 0.05 });
var h = floor_1.default((diff - d * constants_1.secondsPerDay) / constants_1.secondsPerHour, { tolerance: 0.05 });
var m = floor_1.default((diff - d * constants_1.secondsPerDay - h * constants_1.secondsPerHour) / constants_1.secondsPerMinute, { tolerance: 0.05 });
var s = floor_1.default((diff - d * constants_1.secondsPerDay - h * constants_1.secondsPerHour - m * constants_1.secondsPerMinute), { tolerance: 0.05 });
if (d > 90) {
text.push(formatDate_1.default(input, ymdFormat));
sign = 0;
}
else if (d > 30) {
text.push(formatDate_1.default(input, mdFormat));
sign = 0;
}
else if (d > 0) {
text.push(plural_1.default('day', d));
if (d < 4 && h > 1)
text.push(plural_1.default('hour', h));
}
else if (h > 0) {
text.push(plural_1.default('hour', h));
if (h < 4 && m > 0)
text.push(plural_1.default('minute', m));
}
else if (m > 0) {
text.push(plural_1.default('minute', m));
if (m < 4 && s > 0)
text.push(plural_1.default('second', s));
}
else if (s > 0) {
text.push(plural_1.default('second', s));
}
if (sign === -1)
text.push('ago');
if (sign === 1)
text.push(d || h || m || s ? 'from now' : 'now');
return text.join(constants_1.space);
}
exports.relativeTime = relativeTime;
exports.default = relativeTime;