everyutil
Version:
A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.
24 lines (23 loc) • 692 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatRelativeDate = void 0;
/**
* Formats a date relative to today (e.g., "3 days ago").
* @author @dailker
* @param {Date} date
* @returns {string}
*/
function formatRelativeDate(date) {
const now = new Date();
const diff = Math.floor((date.getTime() - now.getTime()) / (1000 * 60 * 60 * 24));
if (diff === 0)
return 'today';
if (diff === 1)
return 'tomorrow';
if (diff === -1)
return 'yesterday';
if (diff > 1)
return `in ${diff} days`;
return `${-diff} days ago`;
}
exports.formatRelativeDate = formatRelativeDate;